Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

'prev' in for loop

by kulls (Hermit)
on Dec 06, 2005 at 09:22 UTC ( #514460=perlquestion: print w/replies, xml ) Need Help??

kulls has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
In for loop, i can able to move my index to either 'last' or 'next' .
I don't know why it's throwing error, when i use 'prev' for accessing prev index.
Is there any specific reason behind this?.Can anyone tell me any suggestion or alternative for this?
Update :
yes,
i understand that there is no 'prev' in for loop.When i was trying to move the index in backward i got this error which is similar kind of action like next if($_ eq 'foo ) , instead prev if($_ eq 'foo').why not this applicable in perl is my question?
-kulls

Replies are listed 'Best First'.
Re: 'prev' in for loop
by gjb (Vicar) on Dec 06, 2005 at 09:28 UTC

    prev simply isn't Perl. next and last are though.

    You can always rewrite a for-loop from:

    foreach my $item (@list) { something($item); }
    to
    for (my $i = 0; $i < @list; $i++) { something($list[$i]); }
    By modifying $i you can get to the "previous" iteration (something like $i--. However, be careful not to create an infinite loop.

    Hope this helps, -gjb-

Re: 'prev' in for loop
by phaylon (Curate) on Dec 06, 2005 at 09:28 UTC
    I'd say the reason for the error is that there is no "prev" statement in Perl. You might want to give a more detailed description of what you want to do, so people can help you find a solution for your problem.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: 'prev' in for loop
by rinceWind (Monsignor) on Dec 06, 2005 at 10:45 UTC

    To get the perl verb 'prev', you need to install Acme::ChronoNaught, which has useful applications for horse racing and the stock market ;).

    Seriously though, I think you have misunderstood the operation of the verbs 'last' and 'next'. The three verbs 'last', 'next' and 'redo' are control verbs - glorified goto. They cause execution to jump: out of the loop, into the next iteration, and to the start of the current iteration, respectively.

    If you want the previous value of your index, you need to store it somewhere - in $prev declared outside the scope of the loop. Set this at the end of your loop, and next time round it will hold what you want.

    Of course, if you are counting, you can always subtract 1.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Of course, Acme::ChronoNaught is the only complex module which was released with absolutely no bugs, as the developers were able to process all bug reports before releasing the module.

      Plus, they can devote all their time to Perl advocacy, since their tests were useful for investment planning.

      emc

      Netlib
Re: 'prev' in for loop
by dragonchild (Archbishop) on Dec 06, 2005 at 10:43 UTC
    There is a 'redo', which will redo the current loop. However, moving backwards an index is ... odd. Why would you want to? You've already seen it, you could could keep track of it in another data structure.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: 'prev' in for loop
by tphyahoo (Vicar) on Dec 06, 2005 at 10:39 UTC
    Like everyone said, it's not part of perl. Here's the cheater's to tell if a keyword / function / "thing" is part of perl core syntax or not: perldoc -f keyword. Works probably > 95% of the time.
Re: 'prev' in for loop
by Perl Mouse (Chaplain) on Dec 06, 2005 at 11:43 UTC
    You're not "moving your index" to the last one when using last. last will terminate the loop immediately. next will terminate the current iteration, which will cause the condition to be tested again. This may cause the end of the loop, or its "next" iteration, with any side-effects of the guard in effect. redo will restart the current iteration, without any testing of the condition.

    I've no idea what prev should do. Rollback the current iteration?

    Perl --((8:>*
      hi,
      I updated my question too.if my loop supports  next if($_ eq 'foo') then, why not it's support  prev if($_ eq 'foo')?
      -kulls
        Because that's how Perl was designed. It would IMO be more confusing than supportive to the programmer. Also, like Perl Mouse said, last and next don't influence the loops position (directly), more the control flow. 'last' exits the block, so the calling run was the /last/ one. 'next' jumps to the end of the block so the /next/ run will start if there is any. 'redo' redoes the block without going to the next iteration.

        Ordinary morality is for ordinary people. -- Aleister Crowley
Re: 'prev' in for loop
by swampyankee (Parson) on Dec 06, 2005 at 11:29 UTC

    What benefit do you see a prev keyword providing?

    From my very limited imagination, the only two benefits I can see are traversing a list backwards (but there are already easier ways to do that) or backtracking in the event of some sort of error. In the latter case, only you would know what state information needs to be kept to make the backtracking useful.

    emc

    Netlib
Re: 'prev' in for loop
by blazar (Canon) on Dec 06, 2005 at 09:52 UTC
    In for loop, i can able to move my index to either 'last' or 'next' . I don't know why it's throwing error, when i use 'prev' for accessing prev index.

    It's throwing an error due to a perl bug: precisely that it is not implemented. Strangely enough neither the prev keyword nor that obvious bug are documented at all...

    The workaround is called $prev.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://514460]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2019-12-09 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?