Perl : In splice() type of arg1 must be array, cannot be scalar dereference. How to fix?
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]开发者_JS百科); # 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.
splice(@$ref, $i, 1); # this works...
Ahhh... I encountered this question last night (2am) so my energy was burnt out...
Now I see the magic in Perl a bit more clearly :)
Sorry about such a simple question.
精彩评论