Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 browsing the Monastery: (19)
As of 2014-07-14 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (268 votes), past polls