next in foreach loop

Hi,

Complete newbie.

Is there any way to use "next" from within a foreach loop?

All the examples I have seen/read use a while loop to demo.

Thanks,
Stuart


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kryten [ Fr, 13 August 2010 01:08 ] [ ID #2046011 ]

Re: next in foreach loop

2010/8/13 Kryten <kryten68 [at] googlemail.com>:
> Hi,
>
> Complete newbie.
>
> Is there any way to use "next" from within a foreach loop?
>

Sure.

$ perl -le '
> for (1..10) {
> next if $_ == 5;
> print;
> } '
1
2
3
4
6
7
8
9
10


--
Jeff Pang
http://home.arcor.de/pangj/

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jeff Pang [ Fr, 13 August 2010 08:39 ] [ ID #2046012 ]

Re: next in foreach loop

On Aug 12, 2010, at 19:08, Kryten <kryten68 [at] googlemail.com> wrote:

> Hi,
>
> Complete newbie.
>
> Is there any way to use "next" from within a foreach loop?
>
> All the examples I have seen/read use a while loop to demo.

Yes, next will work on for/foreach, while, and until loops. So you can say

for my $num (1 .. 10) {
next if $num % 2;
print "$num\n";
}

To print the even numbers between 1 and 10 (inclusive).

The next, last, and redo loop control statements will even work on bare bloc=
ks:

my $i =3D 0;
{
print "infinite loop: ", $i++;
sleep 1;
redo;
}=

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Fr, 13 August 2010 08:44 ] [ ID #2046013 ]
Perl » gmane.comp.lang.perl.beginners » next in foreach loop

Vorheriges Thema: Encoding issues.
Nächstes Thema: Re: GOODBYE from beginners@perl.org