The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 07-17-2008
gholdbhurg gholdbhurg is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 26
how to do sort in perl?

Hi,
Can anyone advise how to correct my script pls?
I'm not getting the correct output =(

Details below:

Logfile
========
Player Num : 14
Player Name : Jacee Thirkins
MVP winner : 1998
End of details....

Player Num : 35
Player Name : Lander Diaz
MVP winner : 2002
End of details....

Player Num : 14
Player Name : Jacee Thirkins
MVP winner : 1999
End of details....

Player Num : 35
Player Name : Lander Diaz
MVP winner : 2001
End of details....

Player Num : 14
Player Name : Oman Rodriguez
MVP winner : 1997
End of details....

Player Num : 35
Player Name : Larry Simpkins
MVP winner : 2003
End of details....

Player Num : 14
Player Name : Jacee Thirkins
MVP winner : 1996
End of details....

Player Num : 35
Player Name : Lander Diaz
MVP winner : 2002
End of details....

Player Num : 14
Player Name : Stephen Jabinzki
MVP winner : 1999
End of details....

Player Num : 35
Player Name : Lander Diaz
MVP winner : 2004
End of details....

Player Num : 35
Player Name : Larry Simpkins
MVP winner : 2005
End of details....


Script
=======
open (INFILE,"$inputfile");
open (OUTPUT, ">$outfile");

my %player_count;
my @all_player;

while ($cur_rec = <INFILE>)
{
if ( $cur_rec =~ m/Player Name/ )
{
@record = split(/:/, $cur_rec);
$playername = "$record[1]" . "$record[2]";
push(@all_player, $playername);
}
}

@sorted_player = sort @all_player;
for $player (@sorted_player)
{
if ("$player" eq "$prev_player" || $ctr == 0 )
{
$ctr++;
}
else
{
$player_count{$ctr} = $player;
$ctr=1;
}
$prev_player = $player;
}

$countr = 0;
for $count (keys %player_count)
{
if ( $countr <= 4 )
{
print "\nPlayer: $player_count{$count} MVP awards: $count\n";
$countr++;
}
else
{
$player_count{$player} = $ctr;
$ctr = 0;
}
}

close (TEST);
close (OUTPUT);


Expected output (should be in order who got the most awards in total):


Top5 MVP winners
===============
Lander Diaz
Jacee Thirkins
Larry Simpkins
Oman Rodriguez
Stephen Jabinzki