Using scalar() function to interpret an array variable in scalar context to get its length. : scalar « System Functions « Perl






Using scalar() function to interpret an array variable in scalar context to get its length.

     

$number = scalar( @array );

#!/usr/bin/perl -w

# Two ways to get the length of an array.

@array = (1,2,3,4,5,6,7,8,9);

print "@array\n";

$number = $#array + 1;

print "Number elements: $number.\n";


# Alternate method.
$number = scalar( @array );

print "Alternate method: $number.\n";

   
    
    
    
    
  








Related examples in the same category

1.Is scalar readonly
2.Using scalar function to convert array to scalar variable