Hi Everyone,
a.txt
cola,colb1111
601030,CEL
6031,CEL
60103,CEL
Would like to find the longest field colb1111, and shortest, so the output is 6, 4
my code:
#!/usr/bin/perl
use strict;
use warnings;
my %prefix_to_contry = ();
my $key;
open(FH,'/root/a.txt') or die "$!";
while(<FH>){
chomp;
my @fields = split(/,/);
$prefix_to_contry{$fields[0]} = $fields[1];
}
my ( $shortest, $longest ) = ( sort { $a <=> $b } map { length } keys %prefix_to_contry )[ 0, -1 ];
print "$shortest $longest\n";exit;
but the output is shortest=4, longest=9. where is wrong with this code.
Thanks
---------- Post updated at 08:03 PM ---------- Previous update was at 11:36 AM ----------
any advice, thanks
---------- Post updated at 09:11 PM ---------- Previous update was at 08:03 PM ----------
find the anwer already.
before chmop, add next if $. < 2;