Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Hash and count of data values

by gjb (Vicar)
on Aug 18, 2003 at 13:51 UTC ( [id://284569]=note: print w/replies, xml ) Need Help??


in reply to Hash and count of data values

You try and print a single value of the hash since the print statement is not in a loop construction.

foreach my $key (keys %hash) { print "$key $hash{$key}\n"; }
should print the collected counts.

Incidently, I don't see why you open the data file twice since you don't read any data the second time.

Personally I'd put the data in the hash while reading since this avoids slurping the whole file which can potentially be large.

my %hash; my $file = 'filehere.txt'; open(DATA, "$file") || die "Can not open: $!"; while (<DATA>) { chomp($_); $hash{$_}++; } close(DATA);

Hope this helps, -gjb-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found