|
|
sybperl-l Archive
Up Prev Next
From: Pradeep Moondra <pradeep at monsoon dot ltindia dot com>
Subject: Re: break statement
Date: Oct 14 1997 3:25AM
Hi,
I think following explanation would help:
PERL doesn't support "break statement" within "for loop". Instead of
break it supports "last [label]", where
label is label of the block(for loop in your case) and is optional.
I tried running your code using last operator and it worked properly.
Your code is comparing with wrong array @e....
Changed code:
----------------
#!/opt/gnu/bin/perl
@a = (123,124,125);
MAIN: {
foreach $i (0 .. $#a) {
if ($a[$i] == "123") {
$code = "A";
}
elsif ($a[$i] == "124") {
$code = "B";
last MAIN;
}
elsif ($a[$i] == "125") {
$code = "C";
}
}
}
Bye,
Pradeep
Gayatri Krishnan wrote:
> Hi,
> Can someone help me with the 'break statement with in a
> 'foreach'
> loop???
>
> I've an array @a with numbers (123, 124, 125)
>
>
> foreach $i (0 .. $#a) {
> if ($e[$i] == "123") {
> $code = "A";
> }
> elsif ($e[$i] == "124") {
> $code = "B";
> break;
> }
> elsif ($e[$i] == "125") {
> $code = "C";
> }
> }
>
> Problem is the loop doesn't break when 'a' is 124... instead
> it goes
> thru all the conditions and $code gets assigned C.
>
> Thanks!
>
> gayatri
|