Perl : In splice() type of arg1 must be array, cannot be scalar dereference. How to fix?
- by Michael Mao
I am trying to comprehend the reference/dereference system in Perl.
What I am trying to do is to remove an element by using reference:
my $ref= \@{$collection{$_[0]}};
# delete($$ref[$i]); # delete works, I've tested that already
splice($$ref, $i, 1); # this wouldn't do.
I first tried the delete() subroutine, it works; however, it doesn't shift the index after the removed elements forward by 1, so I cannot continue working on other stuff.
I then Googled and found the splice() subroutine which does delete and shift in one go.
But the error feedback tells me that
"Type of arg 1 to splice must be array (not scalar dereference)..."
I then tried something like this:
splice(@{$$ref}, $i, 1);
That resulted in another error like this:
"Not a SCALAR reference at...(pointing at that line)"
So I am a bit puzzled, how could I handle this issue?
I prefer not using any CPAN or additional library for the solution, if possible.