Finding the length of an array must be an unusual thing for Perl
programs to do,
because Perl doesn't have an operator for it. It does
have the evil $#:
You may find the length of
array @days by evaluating $#days, as in csh. However, this
isn't the
length of the array; it's the subscript of the last element,
which is a
different value since there is ordinarily a 0th element.
Huh? What does this mean? And is Perl really modelled after csh?
Let's try to do something simple, see how
many arguments were passed to our program:
#!/usr/bin/perl -w
Now let's run it...
print "\$\#ARGV is $#ARGV\n";
/tmp/argv.pl
Huh? -1? This must have been confusing to others too, because it's
documented again in the docs for @ARGV
$#ARGV is -1 /tmp/argv.pl two arguments $#ARGV is 1
$#ARGV is generally the number of arguments
minus one, because $ARGV[0] is the first argument, not
the program's command name itself.
I realize the simple rule is 'the length of @array
is $#array+1', but how dumb is that?
Update:
a friend pointed out you can get length by evaluating
@array in scalar context. Contexts are one of those horrible
features in Perl that make me have to relearn the language every time
I write a program. There's more than one way to do it but
none of them are simple.
|