Use operator $# in Perl
August 13th, 2008 Posted in Perl
In the last few weeks I have been developing a lot of scripts in Perl, I’m not a fan of this scripting language but is very useful in some cases, but I had some troubles with arrays and list in Perl , and today I saw a mistake in my code, and that was how I was using the operator $#.
The $# operator will get the index value of last element in a specific array, and It will not return the length of the array this was my mistake , that’s what happend when you don’t read the documentation :-s.
A simple example how to use this operator.
@names = ("popov", "phelps", "thorpe","smith");
$value = $#names; ## $i is now 3
And to find out the length of an array use the function scalar
@names = ("popov", "phelps", "thorpe","smith");
$value = scalar(@names); ## $i is now 4
I will write more perl post , and some troubles I had in the last few days.
Tags: Perl