Del
05-05-1999, 06:18 PM
Okay, I've got two ways to do the same thing. I've been staring at Perl long enough now, and thinking ahead about what to do next, that I need to ask. (code snippets below, then question)
First,
my @array = <KID_TO_READ>;
close(KID_TO_READ);
my ($found, $html);
foreach $found (@array) {
[nbsp][nbsp][nbsp][nbsp] if($user_found =~ /^$pattern$/i) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$html = "$pattern was found.\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]last;
[nbsp][nbsp][nbsp][nbsp] } else {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$html = "$pattern was not found.\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]next;
[nbsp][nbsp][nbsp][nbsp] }
}
print $html;
Second,
while(<KID_TO_READ>) {
[nbsp][nbsp][nbsp][nbsp] if($_ =~ /^$pattern$/i) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]print "$pattern was found\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]close(KID_TO_READ);
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]exit;
[nbsp][nbsp][nbsp][nbsp] }
}
print "$pattern was not found.\n";
Now for the question. Which one would be faster and/or more efficient? Say <KID_TO_READ> (and therefore @array) have over two thousand elements. I'm thinking the <font color=#FF0000>while(<FH>)</font> would be better because Perl wouldn't have to store the whole list in memory, but I'm not totally sure...
Del<!-- NO_AUTO_LINK -->
[This message has been edited by Del (edited 05-05-99)]
First,
my @array = <KID_TO_READ>;
close(KID_TO_READ);
my ($found, $html);
foreach $found (@array) {
[nbsp][nbsp][nbsp][nbsp] if($user_found =~ /^$pattern$/i) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$html = "$pattern was found.\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]last;
[nbsp][nbsp][nbsp][nbsp] } else {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$html = "$pattern was not found.\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]next;
[nbsp][nbsp][nbsp][nbsp] }
}
print $html;
Second,
while(<KID_TO_READ>) {
[nbsp][nbsp][nbsp][nbsp] if($_ =~ /^$pattern$/i) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]print "$pattern was found\n";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]close(KID_TO_READ);
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]exit;
[nbsp][nbsp][nbsp][nbsp] }
}
print "$pattern was not found.\n";
Now for the question. Which one would be faster and/or more efficient? Say <KID_TO_READ> (and therefore @array) have over two thousand elements. I'm thinking the <font color=#FF0000>while(<FH>)</font> would be better because Perl wouldn't have to store the whole list in memory, but I'm not totally sure...
Del<!-- NO_AUTO_LINK -->
[This message has been edited by Del (edited 05-05-99)]