perlinacan has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to extract teams results from a large player file after managing to retieve and parse the file with LWP I am now stumped on a simple extraction, possibly i have overcomplicated the hell out of it. score is like BradScoll,14, and the player file is like BradScoll
#!/usr/bin/perl # #Program to check and return 10 players score from a large list # # $file = 'score'; # file in format player,score $player = 'player' ; #file in format player open(INFO,"$score" ); #open file with 100 players and scores open(INFO2, "$player" );# Open the file with 10 players to get thier s +cores $index = 0; @total = (<INFO>); while (<INFO2>){ @score = split(/[,]/,$_); if ($total[$index] == $score[0]) { print "$total[$index] , $score[0] ,\n"; $total++; } }
but it doest work. to extract a team list I was trying the follwoing

Replies are listed 'Best First'.
Re: help with extracting data
by grep (Monsignor) on Apr 16, 2002 at 05:28 UTC
    There are several problems with your code.
    open(INFO,"$score" ); # you need to check to see if this failed or not open(INFO,"$score" ) or die "failed to open $score\n"; open(INFO2,"$player" ) or die "failed to open $player\n"; @score = split(/[,]/,$_); # you have a useless character class and # split uses $_ if nothing is specified # this is not actually wrong it is just good + style @score = split(/,/); # but the split is not working on the file # you think it is. # This is splitting $_ from INFO2 which is t +he player file. # It looks like you want the score file. # This confusion most likely brought on by y +our # inconsistant indentation style if ($total[$index] == $score[0]) # You never increment $index. it's al +ways 0 $total++; # I think you meant $index but you created a # new var called $total and incremented it

    As you notice you have many things to fix. You will hear quite a few monks harp about use warnings (-w), use strict and check file open's, the reason is simple every one of your errors would have been evident if these checks were in place.



    grep
    Unix - where you can thrown the manual on the keyboard and get a command

Re: help with extracting data
by Ovid (Cardinal) on Apr 16, 2002 at 05:53 UTC

    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.

Re: help with extracting data
by graff (Chancellor) on Apr 16, 2002 at 06:12 UTC
    The opening comment line (purpose of script) is a VERY good thing - keep that habit, and always start each new script with such commentary. Then, before writing the actual code, expand the comment to explain briefly how the program will accomplish the purpose. (Many times, the commentary may be as long as the code, or longer -- that can be a Good Thing.) For example:
    # First, read the long list of players and scores into a hash # Then, read the short list of important players, and print # their scores from the hash open( LIST, "score" ) || die "can't open score file\n"; while (<LIST>) { chomp; ($name,$score) = split(/,/); $scores{$player} = $score; } open( TEAM, "player" ) || die "can't open player file\n"; while (<TEAM>) { chomp; print "$scores{$_} $_\n"; }

    There are ways to write the code more compactly, of course.

    Is there any chance of two different teams having players named "John Smith" or some such? This would make it more challenging -- the large "score" file would need to be structured to make it clear who's on each team, and you would need to use that structure when reading it. (But then, if you're actually just interested in members of a chosen team, then you don't need a file listing that set of names -- you just need the name of the team.)

      OOPS! Really sorry about that last bit of code I suggested... it should have read:
      ... ($name,$score) = split(/,/); $scores{$name} = $score; ... while (<TEAM>) { chomp; if (exists($scores{$_})) { print "$scores{$_} $_\n"; } else { print "$_ is not listed in score file\n"; } }
Re: help with extracting data
by Anonymous Monk on Apr 16, 2002 at 06:34 UTC
    Thanks for the excellent examples, I am writing this for footy tipping thing I am doing with some workmates, I do plan on going through each example to see which works best and trying to understand why thanks again