gibsonca has asked for the wisdom of the Perl Monks concerning the following question:
I am still struggling a bit with hashes. I want to count the number of strings of the form, "FREQ <val>" in a set of files. I can see that in fact I did count the strings, just not sure how to print the results.
use strict; use warnings; use Env; use integer; use Data::Dumper; my( %cnt, $file, @files, $key, $line, ); @files = <*.abc>; foreach $file (@files) { open (FILE, "$file"); my @contents=<FILE>; close FILE; my @freqs = grep(/ FREQ /,@contents); foreach $line (@freqs) { $line =~ s/ is begin.*//; $line =~ s/.* FREQ/FREQ/; #print "$file $line"; chomp($line); # start counting if (defined $cnt{$line}) { $cnt{$line}{c} = $cnt{$line}{c} + 1; } else { $cnt{$line}{c} = 1; $cnt{$line}{val} = $line; $cnt{$line}{file} = $file; } } # Print out results per file, all the found FREQs: # ( Definitely where I get it wrong starts here ) for $key ( keys %($cnt->{file}) ) { print "key $key \n"; } }
The intended results should be something like the following:
file1.abc FREQ 12.3 5 file1.abc FREQ 5 2 file2.abc FREQ 33.3 12
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: print out the values from a hash
by choroba (Cardinal) on Jan 11, 2013 at 17:52 UTC | |
|
Re: print out the values from a hash
by Kenosis (Priest) on Jan 11, 2013 at 18:11 UTC |