Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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);

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2016-10-28 10:18 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (379 votes). Check out past polls.