Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Match Problem

Status
Not open for further replies.

goliath

MIS
Aug 18, 1999
62
US
Two questions and code below.

What is the best way to escape a While loop if the desired results is met before the while completes?

The script below is looking for a key phrase "Assign Printers" which it does find and then stops as I had hoped, but it finds the second occurance instead of the first.

# while info come from the IPF handle parse
while ($line = <IPF>)
{
chomp;
$dta = $line;
if ($dta =~ /Assign Printers/i) { # if &quot;Assign Printers&quot; found, end processing
print &quot;Ending, Assing Printers Section Found&quot;;
$line = &quot;&quot;; # end while loop
}
}

Thanks for any help!
 
Ahh!

After a little more output checking, I am properly finding the first instance of my parse - the problem is I'm not exiting the &quot;While&quot; loop.

So I'm still stuck on the propper way to exit a while loop if another condition dictates that further processing is not necessary.


Thanks!
 
Use &quot;last&quot; inside your if condition, like so

if ($dta =~ /Assign Printers/i) {
#if &quot;Assign Printers&quot; found, end processing
print &quot;Ending, Assing Printers Section Found&quot;;
$line = &quot;&quot;;
last; # end while loop
}

Something I see, though it might not matter in this case:
when you use chomp as you are here with no argument, it
acts on the $_ variable, which is the current record from
filehandle IPF, not on the variable &quot;$line&quot;. If you want to chomp $line, you need to say &quot;chomp($line);&quot;
 
Thank You !

*happy happy joy joy*

(tosses you a beer)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top