Okay, just a quick one before I head off to bed. First, grep's points were spot on. Second, I went ahead and recoded this the way I think you were intending it. I grab the players and stick them in an array. I grab the scores and stick them in a hash. Then, I create another hash and use a hash slice (with players as the key) to match listed players with their scores. I tested it on a couple of small data files and it seems to work fine.

#!/usr/bin/perl -w use strict; my $team_file = shift || 'player'; # you can specify a team file on th +e command # line or take the default my $scores = 'score'; # get players open TEAM, "< $team_file" or die "Cannot open $team_file for reading: +$!"; # the grep ensures that we actually have data chomp( my @players = grep /\w+/, <TEAM> ); close TEAM; # get scores open SCORE, "< $scores" or die "Cannot open $scores for reading: $!"; my %all_scores = map {chomp;split/,/} grep { /[^,]+,[^,]+/ } <SCORE>; close SCORE; # get player scores for listed players my %player_scores; @player_scores{ @players } = @all_scores{ @players }; while ( my ( $player, $score ) = each %player_scores ) { print "Player: $player\tScore: $score\n"; }

The caveat here, though, is to understand what I did as opposed to just using it. You can learn a fair amount of stuff that would be beneficial for good programming practices.

Incidentally, you might wonder why I threw that map and grep statement in there. This is because, on the off chance this is homework, I want something that you can't explain to an instructor (no offense). If, on the other hand, you need this for a personal or work problem, then it should work just fine.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Re: help with extracting data by Ovid
in thread help with extracting data by perlinacan

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.