LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-21-2004, 03:49 PM   #1
eastsuse
Member
 
Registered: Dec 2003
Location: Pacific Palisades
Distribution: Suse 9.0
Posts: 48

Rep: Reputation: 15
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.
 
Old 12-22-2004, 02:49 AM   #2
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM
use php script to parse a file. blackzone Linux - Software 1 07-07-2004 04:43 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
parse iptables logs - Perl Question toovato Programming 4 10-30-2003 10:56 PM


All times are GMT -5. The time now is 12:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration