in reply to Re^2: Perl Array - What an array contains
in thread Perl Array - What an array contains
There's yet another pitfall associated with the use of $#array to determine the number of elements in an array. It's possible to assign to $[ (see perlvar) to change the base index of Perl arrays. Doing so will result in your source entry appendage(s) receiving a sharp whack, so Don't Do That!SM
Of course, it's possible to use $#array for this purpose in a base-agnostic way:
c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " perl array base is 0 A: array () is empty c:\@Work\Perl\monks>perl -wMstrict -le "$[ = 1; print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " Use of assignment to $[ is deprecated at -e line 1. perl array base is 1 A: array () is empty
But an array @array evaluated in scalar context always yields the number of array elements, so I don't understand why one would ever do this in any other way.
BTW: It's possible to assign a number other than 0 or 1 to $[, but then you're really on thin ice, so Double-Plus-Don't Do That!
Give a man a fish: <%-{-{-{-<
|
|---|