Q.
I have a file in the following manner:
word word word 12
word word word 67
word word word 23
word word word 14
…. …. …. …..
…. …. …. …..
…. …. …. …..
Now I want a perl script to put the 4th field in ascending order.
The output should be:
word word word 12
word word word 14
word word word 23
word word word 67
…. …. …. …..
…. …. …. …..
…. …. …. …..
A. The input to this procedure is the array of all the lines, i.e. each line of the file is an array element.
sub sort_tricks
{
my (@iopt_tricks ) = @_;
my ($large) = “”;
my (%hsh) = ();
foreach ( @iopt_tricks )
{
#### form a hash with the 7th element i.e. time as the key ####
#### all elements having the same key will form the value of the same hash element ####
chomp;
push @{ $hsh{${ [ split ] }[7]} }, “$_\n”;
}
@iopt_tricks = ();
foreach ( sort {$b <=> $a} (keys %hsh) )
{
print “$_ :: @{$hsh{$_}}”;
push(@iopt_tricks, @{$hsh{$_}});
}
return (\@iopt_tricks);
}