$#boo

As we all know $#boo returns the last index of array @boo.

It is clear why we have the prefixes '$' and '@' ('$' is like the first
letter of the word 'scalar' and the '@' is like the first letter of the word
'array').

But is it unclear why there is '#' after the dollar sign. I've checked out
the perl v 1.0 and in the man page there is such text:

> you may find the length of array @days by evaluating "$#days", as in csh.
> [Actually, it's not the length of the array, it's the subscript of the last
> element, since there is (ordinarily) a 0th element.]

So the answer why the number of the last index is $#boo is somewhere is csh.

6 Comments

I always use:

my $num_elements = scalar(@foo);

Sorry Ivan, that was just a quick post before I left for work this morning. Had I had more time I would have mentioned that I have thus always taken the the `$#` prefix to read as 'the scalar number of the array'. I wasn't sure, given your name and location, if you were aware of the English language meaning of the symbol. Since it appears that you are, and that you are looking for something more exactly to do with the origins of Perl, I can't help. Good luck, I like little curios of this sort.

The reason: that's the way it is in the shell.

Leave a comment

About Ivan Bessarabov

user-pic I blog about Perl.