in reply to Re: Count number of occurrences of a list of words in a file
in thread Count number of occurrences of a list of words in a file
How about this then?
#!/usr/bin/perl use 5.18.3; use warnings; chomp (my @words = <DATA>); my %cnt = map { $_ => 0 } @words; foreach my $tf (glob "*.log") { printf STDERR " %-40s\r", $tf; open my $fh, "<", $tf or next; while (<$fh>) { $cnt{$_}++ for grep { exists $cnt{$_} } m/(\w+)/g; } } printf "%-20s : %6d\n", $_, $cnt{$_} for sort { $cnt{$b} <=> $cnt{$a} +} @words; __END__ tux dromedary camel dream milk druid monk wizard azaghal perl
I was more wondering about case sensitiveness
I timed that against the original approach. My code (with 8 words): 19 seconds, OP code: 57 seconds. That difference will exponentially grow with the number of words: with 23 words 20 seconds versus 175 seconds. Total text size was 273 Mb.
|
|---|