Perl : In splice() type of arg1 must be array, cannot be scalar dereference. How to fix?

Posted by Michael Mao on Stack Overflow See other posts from Stack Overflow or by Michael Mao
Published on 2010-05-30T00:43:21Z Indexed on 2010/05/30 0:52 UTC
Read the original article Hit count: 164

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about beginner

Related posts about perl