I am not sure why this Perl sorting is not working.
Please suggest how to resolve this.
while (<>) {
chomp;
if (/VIOLATE/) {
@lines = split " ", $_;
#print "$lines[-2]\n"; ## Print last but one column
my @viol = "$lines[-2]\n";
@sorted = sort {$a <=> $b} @viol;
print "@sorted";
}
};
Command : perl test.pl test.log
test.log :
0.98 2.04 -1.106 VIOLATE
0.98 2.04 3.06
0.98 2.04 -11.06 VIOLATE
0.98 2.04 -1.06 VIOLATE
0.98 2.04 1.06
0.98 2.04 -0.226 VIOLATE
0.98 2.04 -2.06 VIOLATE
Are you trying to match any line with
VIOLATEin it, put the result in an array then sort all the violations? If so you need to declare and sort@violoutside the loop:This outputs:
-11.06 -2.06 -1.106 -1.06 -0.226