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-
| [reply] [d/l] [select] |
Re: 'prev' in for loop
by phaylon (Curate) on Dec 06, 2005 at 09:28 UTC
|
| [reply] |
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)
| [reply] |
|
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.
| [reply] |
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:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] |
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. | [reply] [d/l] |
Re: 'prev' in for loop
by Perl Mouse (Chaplain) on Dec 06, 2005 at 11:43 UTC
|
| [reply] |
|
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
| [reply] [d/l] [select] |
|
| [reply] |
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.
| [reply] |
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.
| [reply] [d/l] [select] |