Stuart White wrote:
> 
> I am reading in a file of one line sentences, and then
> selecting to store several sentences into an array
> based upon the presence of some key words.  I then
> want to assign the array to a hash.  The output of the
> array will look something like this:
> 
> Player1: 1
> Player2: 1
> Player3: 1
> Player1: 2
> Player1: 3
> Player4: 1
> Player3: 2
> Player2: 2
> 
> If this were the array in total, I would expect, or at
> least want my hash to look like this (given that it
> just happens to be sorted alphabetically):
> 
> Player1: 3
> Player2: 2
> Player3: 2
> Player4: 1
> 
> is that a correct expectation?
> Also, to get the numbers to the right of the colon,
> I'd have to have a count for each occurrence of each
> player, how might I do that?
> The code I have so far is below.  I didn't include
> anything with hashes because everything I tried so far
> gave me unexpected and unwanted results.
> 
> So, how do I assign an array into a hash?
> by assigning an array to a hash, it will overwrite
> common key values, correct? (Player1, Player2, etc)
> How can I maintain a separate count for each player's
> line? (A player's line is a line inwhich his name
> appears in a selected line)

You should probably use an array to keep the correct order and a hash to
keep the count:

#!/usr/bin/perl
use warnings;
use strict;

open STATS, "stats.txt" or die "Cannot open stats.txt: $!";

my @order;
my %count;
while ( <STATS> ) {
    next unless my ( $player ) = /^([^:]+):/;
    push @order, $player unless $count{ $player }++;
    }

for ( @order ) {
    print "$_: $count{$_}\n";
    }

__END__


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to