in reply to Grade me! TAC
my $argCount = $#ARGV+1;
To find the number of elements in an array, use the array in scalar context.
my $argCount = @ARGV;
To find out how this is different (and why it is the *correct* way) read about the variable $[ by doing a perldoc perlvar
The sample code will illustrate that.
use strict; $[ = 2; my @num = ( 1..4 ); print "No of elements >> ", $#num+1, "\n"; print "No of elements >> ", scalar @num, "\n";
Ouput
No of elements >> 6 No of elements >> 4
$# is actually the last subscript of the array and the variable $[ sets the base subscript to 2 (default is zero). To access the elements in @num you will have to use subscripts 2,3,4,5 and that is the reason you get '6' in the first case (5+1) and not '4' which is the actual number of elements.
Agree that when you dont reset $[ both the methods are same. Why use a complex last subscript+1 calculation when there is an easier way to find the number of elements that works in *all* cases ??
|
|---|