in reply to Extracting a slice from an array ref

Advanced PERL Programming by O'Reilly ( page 7 & 8 )

This is the reference as to what you seek!

To sum it up:

my @s = $r->[1..3];

Where [1..3] is treated as a comma seperated expression.

The result of this expression is the last term in the array, in this case [3].

Now we have $r->[3] which of course accesses a specific element from array $r.

As you can now see this is not what you intended!

Your actual (but unrelated) error Argument "" isn't numeric in aelem at ... is from using the range [1..3], where the range [1,2,3] will not produce this error (but a different one!).

regards,
Earle

Replies are listed 'Best First'.
Re: Re: Extracting a slice from an array ref
by chipmunk (Parson) on Aug 16, 2001 at 00:11 UTC
    That is actually not the case.   my @s = $r->[1..3]; In this snippet, 1..3 is not treated as a comma separated expression, resulting in the value 3.

    In fact, 1..3 is in scalar context, and so it is the flip-flop operator. Since the left operand is just a constant, there is an implicit comparison to $., which returns false, resulting in the warning: Argument "" isn't numeric in aelem ... The interesting thing is that 1..3 in scalar context returns "" for false, rather than the special false value consisting of both "" and 0.

    Check out perlop for more on the wacky .. operator.