Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Stuck with hw

by blaze (Friar)
on Apr 03, 2003 at 01:38 UTC ( [id://247648]=note: print w/replies, xml ) Need Help??


in reply to Stuck with hw

hmm..well i cant say you're really clear on what exactly you need, but since you did put some code that shows some kind of effort, ill tell you what i make of your question...even though its homework :)

From what i can tell you have a flat file in the format of "player_one player_two player_one_score-player_two_score", and you just need to get all the players stats, how many they've won, lost and how many draws...i'd try something like this::
#!/usr/bin/perl -w use strict; chomp(my $file = <STDIN>); my (@names,@winners,@losers,@draws); open FILE, "$file" or die "Couldnt open: $!\n"; while(<FILE>){ my($name1,$name2,$scores) = split(/ /,$_); push(@names,$name1,$name2); my($score1,$score2) = split(/\-/,$scores); if($score1 > $score2){ push(@winners, $name1); push(@losers, $name2); } elsif($score1 < $score2){ push(@winners, $name2); push(@losers, $name1); } else { push(@draws,$name1,$name2); } } close FILE; my %nams; for(@names){ $nams{$_}++; } my %wins; for(@winners){ $wins{$_}++; } my %los; for(@losers){ $los{$_}++; } my %dra; for(@draws){ $dra{$_}++; } foreach my $in(keys %nams){ $wins{$in} = 0 unless defined $wins{$in}; $los{$in} = 0 unless defined $los{$in}; $dra{$in} = 0 unless defined $dra{$in}; print "Player: $in - Wins: $wins{$in} - Losses: $los{$in} - Draws +: $dra{$in}\n"; }
This is very untested, so you might have to modify it a bit, if im misunderstanding what you need let me know

-Robert

Update: gave script ability to print a zero

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://247648]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2024-04-19 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found