Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


Update: gave script ability to print a zero

In reply to Re: Stuck with hw by blaze
in thread Stuck with hw by dirtyboy

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-15 12:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found