CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How do you stop a <While> Loop

 



waherne
Novice

Dec 30, 2000, 10:28 AM

Post #1 of 3 (315 views)
How do you stop a <While> Loop Can't Post

Hi folks,

How does one stop the while statement looping unnecessarily through all lines in a file after it has found what it was looking for? I do not want to use 'exit;' as I want the remaining script to do other work once it found something.

Thanks for your help.

Willie

Code Extract:

$target="data.txt";
open(DB, $target) or die "Error opening file: $!\n";
flock DB, $EXCLUSIVE;
while(<DB>)
{
($key,@field_vals) = split(/\|/, $_);
if($field_vals[2] =~ /$something/oi )
{
$found=1;
};

} # End of while.
flock DB, $UNLOCK;
close (DB);



Mortimer
journeyman

Dec 30, 2000, 1:51 PM

Post #2 of 3 (309 views)
Re: How do you stop a <While> Loop [In reply to] Can't Post

You can use `last'.

Code
$target="data.txt"; 
open(DB,"<$target")or die("Error opening file: $!\n");
flock(DB,1);

while(<DB>){
($key,@field_vals) = split(/\|/, $_);
if($field_vals[1] =~ /matchthis/oi ){
print "This matched: - $_ - <em>so let me out.</em>
";
last;
}
print "$_
";
}

close (DB);

print "<em>Thank you.</em> Scripts continues...
";

Dave. (Wannabe Perl hacker).
www.dmscripts.com - davemortimer@one.net.au




waherne
Novice

Dec 31, 2000, 2:10 PM

Post #3 of 3 (298 views)
Re: How do you stop a <While> Loop [In reply to] Can't Post

Thanks Dave,

Willie


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives