Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Hash and count of data values

by gjb (Vicar)
on Aug 18, 2003 at 13:51 UTC ( #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? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-11-30 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?