in reply to literal lists

For instance:
my ( @a, @b ); @a = ( 1, 2, 3 ); foreach ( 0 .. $#a ) { }
Walks the values 0, 1 and 2.
foreach ( 0 .. $#b ) { }
does nothing. But $#b returns -1 because there are no values in it. If it would count down, It'd walk the values 0 and -1.

Ordinary morality is for ordinary people. -- Aleister Crowley

Replies are listed 'Best First'.
Re^2: literal lists
by danmcb (Monk) on Sep 06, 2005 at 12:03 UTC
    true. In that case I wonder why $#b evaluates to -1, rather than undef?

      I guess because undef in an integer context will be taken as '0' (albeit with a warning if you have asked for warnings), whereas -1 is not a valid array index:

      @b = (a .. z); + $#b = undef; + print "@b"; + $#b = -1; + print "@b";

      /J\

        That makes some sense. -1 is a valid index though, in normal use - it gives you the last element. But in the code you gave, the -1 indeed makes @b an empty array (I didn't know you could do that). Kind of like a quick way to truncate arrays.