Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Reading through a file and checking for a specific string

by protist (Monk)
on Aug 20, 2013 at 08:51 UTC ( #1050150=note: print w/ replies, xml ) Need Help??


in reply to Reading through a file and checking for a specific string

I am not sure why you are using so much indexing.

Here is an example that demonstrates finding the two letter combinations at any point in the string.

I didn't want to have to test all the substring stuff, but this should give you an idea of how to avoid all the needless indexing.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @strings = ("AB", "AC", "AD", "AE", "FG"); my $counts; for my $file (glob ('File/*')) { open my $fh, "<", $file || die "Could not open $!!"; for my $line (<$fh>) { for my $string (@strings) { $counts->{$string}++ for $line =~ m/$string/g; } } close $fh; } print Dumper($counts);


Comment on Re: Reading through a file and checking for a specific string
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-07 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls