tags:

views:

85

answers:

1

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.

+2  A: 
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.

Michael Mao
`@{$ref}` would also have worked. `@$ref` is more common, but if you have a more complex expression (instead of just a single variable), you need the braces.
cjm
Yes, see http://perlmonks.org/?node=References+quick+reference
ysth