Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Help, directories?

by Rudolf (Pilgrim)
on Dec 05, 2012 at 15:36 UTC ( #1007308=note: print w/replies, xml ) Need Help??

in reply to Help, directories?

Hello! I am a bit confused about what your trying to do EXACTLY but I made some assumptions. So I am assuming your just searching a bunch of files for a specific line or string and want to know which one/ones contain it. I wrote some code bellow, however since I dont know what kind of files your searching through the regex should be converted to your needs and if you're searching for strings that could be longer than one line, reading line by line won't catch it. Hope this helps atleast a little, unless I misunderstood your intentions, good luck!

use v5.14; my $dir = "C:\\Users\\Desktop\\collection2"; opendir(DR, $dir) || die "could not open directory: $!\n"; my @docs = grep{/[^\.|..]/} readdir DR; closedir DR; while ( chomp(my $query = <>) ) { foreach my $doc (@docs){ open(DOC,'<',"$dir\\$doc") or die "$!\n"; while(<DOC>){ chomp; if($query =~ m/$_/i){ print "$query appears in $doc\n"; } } close DOC; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007308]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-06-18 19:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.