Friday, December 24, 2010

scalar() function in Perl

scalar() is an inbuilt function of Perl responsible for returning the number of
elements present in the array passed to the function

#!/usr/bin/perl

print "content-type: text/html \n\n";

@nums = (1 .. 20); # assigning 1 to 20 in the array sequentially as separate elements
@alpha = ("a" .. "z"); # assigning a to z in the array sequentially as separate elements
$numofnums = @nums;

print "@nums\n";

print "@alpha\n";

print "There are $numofnums numerical elements";

print "There are ".scalar(@alpha)." letters in the alphabet!";

Output:
-------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z
There are 20 numerical elements
There are 26 letters in the alphabet!