Because the latter (grep) is essentially a loop,so grep() is not better than
using 'while(<>)'.Specially,when the file's size is large,reading all the
contents of this file into an array would consume your memory quickly.
If your /search_pattern/ is changing frequently,I would give another example
for searching:
my @init = qw/xxx yyy zzz/;
my $match_ref=matching(@init);
while(<HD>){
if ($match_ref->($_)){
....
}
}
sub matching
{
my @[EMAIL PROTECTED];
my $eval=join "||", map { "\$_[0]=~m/\$inits[$_]/o" } 0 .. $#inits;
my $ref=eval "sub { $eval }";
die "can't eval: $@" if $@;
return $ref;
}
Here @init strore the conditions of /search_pattern/.
From: "kilaru rajeev" <[EMAIL PROTECTED]>
To: beginners@perl.org
Subject: Searching for a word in a file ....... need a faster way.
Date: Tue, 4 Jul 2006 22:52:06 +0530
Hi all,
I'm searching for a word in a file. I wrote the code like this.
while ( <FH> ) {
if ( /search_pattern/ ) {
executebale.........
}
}
or
Can I use like,
@list = <FH>;
grep();# using grep on list
Let me know a faster way to search.
Regards,
Rajeev
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>