Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,694 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,310 people online right now. Registration is fast and FREE... Join Now!




Why is there a next and a last command?

 
Reply to this topicStart new topic

Why is there a next and a last command?

enigma-paradox
post 19 Jun, 2008 - 07:46 PM
Post #1


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 8

I was reading this section of a perl tutorial and it was talking about loops. This is what it said.
CODE


while(...some loop conditon...)
{
     if(...we want to skip the rest of the code in the loop...)
       {next}
     if(...we want to leace the loop...)
        {last}
}

The next statement says: jump to the end of the loop. It's a shortcut way to end this iteration ans start the next one. A for loop will do its usual end-of-loop processing (running the third statement between the parentheses) just as it does at the end of a normal iteration.

The last statement says: this loop is done The loop stops immediately and the script continues running at the statement after the end of the loop.


what I don't understand is why you would have the next or last commands at all. If you wanted to skip a part of the loop, why would you even put it in the code to begin with? Also, why would you have a last command. If you want your code to stop after 10 iterations (just a random number that i picked) why wouldn't you just include this in the original condition at the beginning of the while loop? Will somebody please explain this?
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 19 Jun, 2008 - 08:30 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,256



Thanked 14 times

Dream Kudos: 650
My Contributions


What if you wanted to loop through an array of 22 objects, and then store the proper value into a different variable? Something like this:
CODE

my $wanted = 0;
foreach my $item(@array) {
if($item == 42) {
  $wanted = $item;
  last;
}
}

It's a nice way to break out of the loop in that sort of situation.
User is offlineProfile CardPM

Go to the top of the page

KevinADC
post 19 Jun, 2008 - 11:16 PM
Post #3


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 213



Thanked 3 times

Dream Kudos: 50
My Contributions


There are many reasons to use loop controls:

next
last
redo

often while looping you have no idea where or when a condition will be true, so you can use the loop controls in a very arbitrary way to increase the efficiency of your code, for example, skip blank lines in a file:

CODE

while (<>) {
   next if (/^\s*$/);
   ....
}


You put the condition to trigger "next" at the start of the loop to avoid unecessary processing of blank lines. Same is true for redo and last. If you code much perl you will use "next" and "last" often. "redo" is not used as often, at least not in my experience, but it comes in handy sometimes.
User is offlineProfile CardPM

Go to the top of the page

enigma-paradox
post 20 Jun, 2008 - 06:02 AM
Post #4


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 8

CODE

my $wanted = 0;
foreach my $item(@array) {
if($item == 42) {
  $wanted = $item;
  last;
}
}


so in this example, if 42 is the second item in the array, the foreach loop will stop instead of going through the other 20 itmes in the array correct?

CODE

while (<>) {
   next if (/^\s*$/);
   ....
}


I don't know what all of the special perl variable are used for yet. I've read about them a little but I still don't understand their uses and meaning. The above code makes no sense to me.
User is offlineProfile CardPM

Go to the top of the page

KevinADC
post 20 Jun, 2008 - 11:25 AM
Post #5


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 213



Thanked 3 times

Dream Kudos: 50
My Contributions


Yes, the loop stops. For a small array this will have little to no advantage, but if you are looping an array with, for example, millions of elements it can be a very dramatic increase in efficiency.

You don't need to know what the code means, its just an example of how "next" can be used to also avoid inefficient processing of a list/array. When you get to regular expressions you will understand the code. For now I suggest you concentrate on the basics: data types, loops, file I/O, operators, and perls builtin functions.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:47AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month