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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.