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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Exiting While Loop Early



Sep 29, 2000, 6:37 PM

Post #1 of 2 (372 views)
Exiting While Loop Early Can't Post


I am running a while loop until an array contains 4 entries, these are taken from a flat file database, by checking dates.

However in some occassions the script may get to the end of file without finding 4 entries.

How do I exit out of the loop - run the rest of the rest of the script with whatever entries it has found. I can set a date in the future and when it gets to this date it will stop but how, my idea but not working

while (@array<4){ #start of while loop

#my attempt at exiting
if ($year="2004"){
} #end of if
} #end of while loop

Obviously there is a lot missing
This exits the script completely
What can I do to use say 2 entries in @array

Many many thanks



Sep 29, 2000, 9:12 PM

Post #2 of 2 (372 views)
Re: Exiting While Loop Early [In reply to] Can't Post

The last() function will remove you from the loop:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

while ($x < 10) {
print $x;
last if $x == 5;

That prints 12345 and the exits the while loop, and continues with the rest of the program.

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


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

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