in reply to Re: Grep-Like Activity
in thread Grep-Like Activity

Hey Thanks for the replies. My whole idea is that I have a folder called C:\myFolder which has some files. Now i want to scan each files' contents in this folder for a particular expression, say myExp.Process that expression and pick a word from it which is a file name. Then with this new file name (ex: a and append .txt to it), I have to search it in a list of directories that will be present in an array with me in perl. Check their presence and if they are present check for the above expression myExp2. Thus in a way there can be recursive search. For this if I scan line-by-line in a perl program then it will just slightly better than a C program. Hence i wanted to search the whole file initially with a command which will take an expression and search the whole file ,something like grep. Please help me on the same. Thanks in advance, Nishith

Replies are listed 'Best First'.
Re^3: Grep-Like Activity
by holli (Abbot) on Mar 04, 2005 at 12:59 UTC
    For this if I scan line-by-line in a perl program then it will just slightly better than a C program.
    You´re wrong. To scan the whole file you will have to slurp it into memory first. That´s bad, especially when the target for myExp is likely to be near the beginning of the file.
    However, here is how to read a file at once and apply a regex to the content:
    use strict; my $data; slurpfile ("yourfile.txt", $data); print $1 if $data =~ /(expression)/m; sub slurpfile { local $/ = undef; open IN, "<", $_[0] or return 0; $_[1] = <IN>; close IN; return 1; }


    holli, /regexed monk/