$
$
$ perl -le '@str = qw( %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c );
for (@str) {$x=$_; $count=s/%//g; if (!defined $min or $count<$min){$min=$count; $elem=$x}}
print "\nElement with the least number of % character => ",$elem'
Element with the least number of % character => ab%c
$
$
I have just one issue with the approach given -
As indicated in the original post I need the string(s) (string or strings) having the least number of %'s.
Please advise/suggest on having the list of strings/string which have/has the least number of %'s.
perl -le'
@str = qw(
ab% %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c
);
do {
$cnt = tr/%//; push @{$cnt{$cnt}}, $_;
$min = $cnt unless defined $min and $cnt > $min;
}
for @str;
print "Element(s) with the least number of % character => ",
join ", ", @{$cnt{$min}};
'
Output:
% perl -le'
@str = qw(
ab% %%abc %ab%c% %a%b%c% ab%c ab%%%%%c ab%%c
);
do {
$cnt = tr/%//; push @{$cnt{$cnt}}, $_;
$min = $cnt unless defined $min and $cnt > $min;
}
for @str;
print "Element(s) with the least number of % character => ", join ", ", @{$cnt{$min}};
'
Element(s) with the least number of % character => ab%, ab%c