in reply to nested for loop with assumed scalars

EDIT: thanks to Jenda

beware with

for my $i (1..$#array) { print "$i: $array[$i]\n"; }
you'll never get the array element $array[0] because 0 is the first index of an array (well, you could change this behaviour by setting the variable $[ to a nonzero value, but then you get problems with $#array, and IMHO $[ should never be changed because in my eyes changing $[ usually causes more problems than it cures). If you also want to get this, you need to do something like
for (0..$#array) { print "$i: $array[$i]\n"; }

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Replies are listed 'Best First'.
Re^2: nested for loop with assumed scalars
by Jenda (Abbot) on Aug 08, 2004 at 23:52 UTC

    I don't understand what do you mean by "problems with $#array". It would still be the index of the last item as defined:

    $[ = 3; @a = (1,2,3,4,5); print $a[$#a];
    but of course I agree it's not a good idea to fiddle with $[.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne