Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The perl program should accept a file name as a command line argument of the form:
Karpov Shirov 3.5-1.5 Shirov Anand 2.0-3.0 Anand Kasparov 1.0-4.0 Kasparov karpov 2.0-2.0 BlueJunior Kasparov 3.0-3.0
The first two strings are names of chess players, followed by the score of the match between them. For example, in the first match Karpov beat Shirov by a score of 3.5 to 1.5.

Your program should read these data and create an associative array, record, indexed by player names. In record{'name'}, you should store the number of wins, losses and draws of player name.

Your program should then produce output for each player, in the form:

player_name, # of wins, # of losses, # of draws and the output should be sorted in descending order of total points.

Note : A player gets 1 point for each win, 0 points for a loss, and 0. +5 points for a draw. *********************************************** THIS FOLLOWING IS THE CODE THAT I HAVE WRITTEN. UW PICO(tm) 4.0 File: red.pl $in_file = shift@ARGV; open(PLAYERS, "<$in_file") || die "Cannot open the input file!"; $p1wins = 0; $p1loss = 0; $p2wins = 0; $p2loss = 0; $p1draw = 0; $p2draw = 0; $wins = 0; $loss = 0; $draw = 0; while(<PLAYERS>) { if (/(\w+)\s+(\w+)\s+(\d\.\d)\-(\d\.\d)/) { if($3 > $4) { $p1wins = $p1wins + 1; $p2loss = $p2loss + 1; $player{$1} = [$1, $p1wins, $p1loss, $p1draw]; $player{$2} = [$2, $p2wins, $p2loss, $p2draw]; } } elsif($3 < $4) { $p1loss = $p1loss + 1; $p2wins = $p2wins + 1; $player{$1} = [$1, $p1wins, $p1loss, $p1draw]; $player{$2} = [$2, $p2wins, $p2loss, $p2draw]; } else { $p1draw = $p1draw + 1; $p2draw = $p2draw + 1; $player{$1} = [$1, $p1wins, $p1loss, $p1draw]; $player{$2} = [$2, $p2wins, $p2loss, $p2draw]; } } } print $player{Kasparov}[0], " ",$player{Kasparov}[1], " ",$player{Kasp +arov}[2]," ",$player{Kasparov}[3],"\n"; print $player{Anand}[0]," ",$player{Anand}[1]," ",$player{Anand}[2]," +",$player{Anand}[]3,"\n"; print $player{Karpov}[0], " ",$player{Karpov}[1], " ",$player{Karpov}[ +2]," ",$player{Karpov}[3],"\n"; print $player{Shirov}[0], " ",$player{Shirov}[1], " ",$player{Shirov}[ +2]," ",$player{Shirov}[3],"\n"; print $player{BlueJunior}[0], " ",$player{BlueJunior}[1], " ",$player{ +BlueJunior}[2]," ",$player{BlueJunior}[3],"\n"
I am really stuck with this, I am not able to get past this. Your help is greatly appreciated.

Thanks.
Arjun.

+ 2loss, $p2draw]; } )

Edit by tye, add formatting


In reply to Stuck with hw by dirtyboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-12 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found