Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Count number of occurrences of a list of words in a file

by Tux (Canon)
on May 09, 2018 at 16:31 UTC ( [id://1214287]=note: print w/replies, xml ) Need Help??


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.


Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-19 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found