Re: exiting a if loop
by fundflow (Chaplain) on Jun 06, 2000 at 19:13 UTC
|
Why use TRUE/FALSE instead of the standard 1/0?
This could be:
$check = ((HOLIDAY) || ($time <= 1800));
| [reply] [d/l] |
Re: exiting a if loop
by nuance (Hermit) on Jun 06, 2000 at 17:55 UTC
|
The if statement already works in the manner that you have
asked for. A cut down version of your code below shows how this works:
if(HOLIDAY){
$check = TRUE;
}
elsif ( $time <= 1800){
$check = TRUE;
}
This does what you asked for and as you can see you don't need the exit statements. Perl evaluates the "if/else if" statements in turn until one of the conditions is true. It then executes the block immediately following that statement and then jumps to the end of the entire conditional. Thus it only does the "first" conditional block that it can. Several other languages don't work that way (c's switch statement for instance needs breaks) and that may have confused you, but Perl already does what you want by default.
You realise of course that you example you gave can be written more succintly as:
$check = TRUE if ((HOLIDAY) or ($time <= 1800))
If this hasn't covered the whole subject to your satisfaction, if you post more of what you're trying to do then we can elaborate.
update As le and flyfishing have pointed out I got the else if thing wrong. I've changed the node so that it reads properly.
Nuance
Baldrick, you wouldn't see a subtle plan if it painted
itself purple and danced naked on top of a harpsichord,
singing "Subtle plans are here again!" | [reply] [d/l] [select] |
|
if(HOLIDAY){
$check = TRUE;
} elsif ( $time <= 1800){
$check = TRUE;
}
| [reply] [d/l] |
|
Which is basically what I wrote, what with white space being irrelevant and all. Hey this isn't Python :-)
Lets face it:
if(HOLIDAY){$check = TRUE;} elsif ( $time <= 1800){$check = TRUE;}
Would also work, even if it is slightly less readable
Nuance
Baldrick, you wouldn't see a subtle plan if it painted
itself purple and danced naked on top of a harpsichord,
singing "Subtle plans are here again!" | [reply] [d/l] |
|
|
|
> $check = "TRUE" if ((HOLIDAY) or ($time <= 1800))
For more succinctness, leave out the parens:
$check = TRUE if $HOLIDAY or $time<=1800;
| [reply] [d/l] [select] |
Re: exiting a if loop
by buzzcutbuddha (Chaplain) on Jun 06, 2000 at 18:57 UTC
|
$check = TRUE if ((HOLIDAY) || ($time <= 1800));
or
((HOLIDAY) || ($time <= 1800)) ? $check = TRUE : $check = FALSE;
;) | [reply] [d/l] [select] |
|
$check = $HOLIDAY || $time<=1800 ? "TRUE" : "FALSE";
[1] TIMTOWTDI, of course, I'm not saying it's
"wrong", just inelegant :)
[2] Note that the use of "or" instead of
"||" would be wrong in this case, as ternary is higher than "or",
but lower than "||", in precedence.
| [reply] [d/l] |
|
I concede that I did not make the best use of the ternary operator. oh well. now I know better.
thanks for the tip Turnstep.
| [reply] |
Re: exiting a if loop
by lhoward (Vicar) on Jun 06, 2000 at 17:00 UTC
|
last The `last' command is like the `break' statement
in C (as used in loops); it immediately exits the
loop in question. If the LABEL is omitted, the
command refers to the innermost enclosing loop.
The `continue' block, if any, is not executed:
LINE: while (<STDIN>) {
last LINE if /^$/; # exit when done with header
#...
}
`last' cannot be used to exit a block which
returns a value such as `eval {}', `sub {}' or `do
{}', and should not be used to exit a grep() or
map() operation.
Note that a block by itself is semantically
identical to a loop that executes once. Thus
`last' can be used to effect an early exit out of
such a block.
| [reply] |
RE: exiting a if loop
by nuance (Hermit) on Jun 06, 2000 at 23:27 UTC
|
Several of the last few posts assign both true and false to
$check. The original statement in the question does not.
Turnsteps post is the last one that got it right, but if you
want to use the ternary operator then this does it:
$check = HISTORY || $time <= 1800 ? TRUE : $check;
Nuance
| [reply] [d/l] |
RE: exiting a if loop
by le (Friar) on Jun 06, 2000 at 17:17 UTC
|
if is not a looping function, so I don't think you can break out. | [reply] |
|
Sure you can. Check the last para in the doc:
Note that a block by itself is semantically
identical to a loop that executes once. Thus
`last' can be used to effect an early exit out of
such a block.
| [reply] [d/l] |
|
An "if block" is not considered a looping construct on it's own, it's an exception, so the loop exit statements don't work. What you can do is make the "if block" contain just a "bare block". Since a bare block is considered a loop then it works, so:
if (true) {
yada;
yada;
next if $foo;
yada;
yada;
}
Doesn't work,
if (true) {
{
yada;
yada;
next if $foo;
yada;
yada;
}
# next comes here
}
Does.
Nuance
Baldrick, you wouldn't see a subtle plan if it painted
itself purple and danced naked on top of a harpsichord,
singing "Subtle plans are here again!" | [reply] [d/l] [select] |
|
| [reply] |