LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   optimizing perl parse file. (https://www.linuxquestions.org/questions/programming-9/optimizing-perl-parse-file-268914/)

eastsuse 12-21-2004 03:49 PM

optimizing perl parse file.
 
I have a large array of strings. If one of those strings matches a line in a file, I want to print it to a new file. I can do this:

while (<FILE>) {

foreach $str (@strings) {
if ($_ =~ m/$str/g) {
print NEWFILE "$_";

}
}


}

while this works, it slows down considerably with large arrays. Is there a more optimized way to search each line of a file and match it to one or more members in an array? I know there gotta be a better and more efficient way.

dustu76 12-22-2004 02:49 AM

Firstly, I'm a total perl newbie. So if I'm wrong somewhere, kindly correct me.

Upon trying your code, in case there is a line which has multiple matching strings get printed multiple times. So if the line has three matching strings, it would get repeated thrice. This may not be what you want:

Example:

Code:

SF1B:/supmis/soumen/tmp> cat c.txt
testing for hello world string
second line with hello
final line with world
and this wont match anything
SF1B:/supmis/soumen/tmp>
SF1B:/supmis/soumen/tmp> cat abc.pl
#!/usr/bin/perl

use strict ;

die "Error : Could not open file for reading $!"
        unless open FILE, "<c.txt" ;

my @strings = ("line with", "hello", "world") ;

while (<FILE>) {
        foreach my $str (@strings) {
                print "$`<$&>$'" if /$str/g ;  ### for debugging only
                #print if /$str/g ;            ### un-comment for actual use
        }
}
SF1B:/supmis/soumen/tmp> abc.pl
testing for <hello> world string
testing for hello <world> string
second <line with> hello
second line with <hello>
final <line with> world
final line with <world>
SF1B:/supmis/soumen/tmp>

This apart, I tried another approach which I suspect may NOT be the best way:

Code:

SF1B:/supmis/soumen/tmp> cat xyz.pl
#!/usr/bin/perl

use strict ;

die "Error : Could not open file for reading $!"
        unless open FILE, "<c.txt" ;

my @strings = ("line with", "hello", "world") ;
my $srcstr = join "|", @strings ;

while (<FILE>) {
        #print          ### Un-comment for actual use
        print "$`<$&>$'" ### for debugging only
                if /$srcstr/g ;
}
SF1B:/supmis/soumen/tmp> xyz.pl
testing for <hello> world string
second <line with> hello
final <line with> world
SF1B:/supmis/soumen/tmp>

In fact, I will be grateful if anyone can point out the optimisation issues in this code.

HTH.


All times are GMT -5. The time now is 09:50 AM.