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. |
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 Code:
SF1B:/supmis/soumen/tmp> cat xyz.pl HTH. |
All times are GMT -5. The time now is 09:50 AM. |