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