PDA

View Full Version : Del's question of the day


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 = &quot;$pattern was found.\n&quot;;
[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 = &quot;$pattern was not found.\n&quot;;
[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 &quot;$pattern was found\n&quot;;
[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 &quot;$pattern was not found.\n&quot;;


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)]

Justin
05-05-1999, 06:30 PM
Actually, the two snippets wouldn't do the same thing...

The first one would print &quot;pattern not found&quot; for each line that doesn't have the pattern... and the second one would print it only after looping through all lines even if the pattern was found...

Here's what I'd do:

my $found = &quot; not &quot;;
while(<KID_TO_READ>) {
[nbsp][nbsp][nbsp][nbsp] if($_ =~ /^$pattern$/i) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$found = &quot; &quot;;
[nbsp][nbsp][nbsp][nbsp] }
}

close(KID_TO_READ);

print &quot;$pattern was&quot; . $found . &quot;found.\n&quot;;


This way, if it is found anywhere in the file it makes the $found variable into only a space, otherwise it leaves it as &quot; not &quot;. Of course you can do this several ways but this seems the quickest and possibly most efficient (that I know of). There's a command to break out of a while loop (is it <font color=#FF0000>break</font> or am I thinking C++?) that I'd put in right after the <font color=#FF0000>$found = &quot; &quot;;</font> part so as to bust out as soon as it's determined that the pattern is in fact in the file...

<edit>Darn them friggin smiley's!!!</edit>

------------------
Justin Nelson
FutureQuest Support<!-- NO_AUTO_LINK -->
[This message has been edited by Justin (edited 05-05-99)]

Del
05-05-1999, 06:43 PM
Justin,

Actually, the two snippets wouldn't do the same thing...

You're right, I found that right after I posted. The edit should make them do the same thing. (I tweaked the snippets a bit from the way they are in the actual program, mostly so they'd fit in a smaller space and not have extraneous info that doesn't matter here. In the real program they produce the same results)

The 'break out' word for Perl is <font color=#FF0000>last</font>. Also available is <font color=#FF0000>next</font>, which will proceed onto the next available $_ (or whatever) if it 'fails'.

Speaking of the smileys, they've got me twice today in code snippets hehehe

Del

Justin
05-05-1999, 07:05 PM
The smiley problem is because I convert all &amp; to &amp;amp; and all &quot; to &amp;quot; etc, keeping with proper HTML formatting, so if you have something like <font color=#FF0000>$ubb = &amp;foo(&quot;bar&quot;);</font> the result is (internally) <font color=#FF0000>$ubb = &amp;amp;foo(&amp;quot;bar&amp;quot;);</font>, noting the bold <font color=#FF0000>;)</font> which then becomes a biggrin... might need to reorder the conversions.. but then the quotes in the smiley image reference would get messed up....

Ah, well, at least we have that kewl lil checkbox :)

<edit>Great - now I get snagged by typing the wrong ubb code tags hehe</edit>

------------------
Justin Nelson
FutureQuest Support<!-- NO_AUTO_LINK -->
[This message has been edited by Justin (edited 05-05-99)]

Del
05-05-1999, 07:26 PM
Great - now I get snagged by typing the wrong ubb code tags hehe
LOL

How hard would it be to disable all smileys and autolinking in the code tag, but not everywhere else? That'd just make one less thing to think about, saving at least the two of us from having to come back and edit the post cause we forgot the disable box :) (prolly more trouble than it's worth I bet, just thinkin out loud)

Your idea for the 'found' checker worked wonderfully, and looks cleaner than mine. Thanks much.

Del