Friday, September 06, 2013

Interesting fact about finding perl array size

Recently, I came across a problem with Perl array. I was assuming that these three methods generate same output for finding array size, but I am WRONG :

my @newArray = (2);
print scalar @newArray;   #1
print $#newArray; #2
print @newArray; #3

First and third give me proper values, but second gave me 0 size. So be careful, while using it. I would recommend using "scalar @newArray".

Here is some explanation on it :


No comments: