On Sat, Nov 03, 2001 at 05:49:37PM +0100, Thomas Hofer wrote:
> Hi!
> 
> As a perl beginner, I have a silly question about 
> array-slices-syntax:
> 
> That's clear:
> 
> perl -e '@a=(a,b,c);print "@a[1..2]\n"'
> ==> b c
> 
> perl -e '@a=(a,b,c);print "$a[1]\n"'
> ==> b
> 
> But why does $array[range] always give the first array-entry?
> 
> perl -e '@a=(a,b,c);print "$a[1..2]\n"'
> ==> a
> 
> (OK, maybe it's useless, but I'd like to understand...)

Really?  OK, then :-)

You are using .. in a scalar context.  This is totally different to
using it in an array context.  In scalar context it is the bistable
operator which returns false while its first operand is false, then true
until its second operand is true.  Read more in perlop.

When either operand to .. is a constant, which would otherwise not make
sense, the operand is compared to $. - the number of the line currently
being read.

So, for you the .. operator is returning false, which is converted to 0.

If you want to see what .. is returning, run this:

$ echo "1\n2\n3\n4\n5\n6" | perl -ne 'print "<", scalar(3..5), ">\n"'
<>
<>
<1>
<2>
<3E0>
<>

These return values are guaranteed.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to