How to find size of an array in Perl and what are the ways to find it?
Method1:
@a=(1,2,9,5,6);
print scalar @arr;
Method2:
@a=(1,2,9,5,6);
print $#a;
print $#a + 1;
Method3:
perl -le '@a=(1,2,9,5,6); print $#a + 1;'
Method4:
my $size = @arr;
print $size;
Method 5:
@a=(1,2,9,5,6);
print @a."\n";
Method 6:
@a=(1,2,9,5,6);
print scalar grep /$_/ ,@a;
Method 7:
@a=(1,2,9,5,6);
print scalar map /$_/ ,@a;
Method 8:
@a=(1,2,9,5,6);
print scalar map $_ =~ s///,@a;
Method 9:
@a=(1,2,9,5,6,8);
print scalar grep $_ =~ s///,@a;
Method 10:
@a=(1,2,9,5,6,8);
print $_= map{$_ =~ s///}@a;
Method 11:
@a=(1,2,9,5,6,8);
print scalar grep{$_ =~ s///}@a;
Method 12:
@a=(1,2,9,5,6,8);
$b=0;
$b+=$_=~s/// for @a;
print $b;
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home