Lhamo_rin has asked for the wisdom of the Perl Monks concerning the following question:
Title edit by tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Switch
by broquaint (Abbot) on Sep 29, 2003 at 13:52 UTC | |
| [reply] | |
|
Re: Switch
by CombatSquirrel (Hermit) on Sep 29, 2003 at 13:53 UTC | |
Output:
How do I create a switch or case statement?
The general answer is to write a construct like this:
Sometimes you should change the positions of the constant
and the variable. For example, let's say you wanted to
test which of many answers you were given, but in a caseinsensitive way that also allows abbreviations. You can
use the following technique if the strings all start with
different characters or if you want to arrange the matches
so that one takes precedence over another, as ""SEND"" has
precedence over ""STOP"" here:
Entropy is the tendency of everything going to hell. | [reply] [d/l] [select] |
|
Re: Switch
by hardburn (Abbot) on Sep 29, 2003 at 14:40 UTC | |
You'll note that the methods noted in above post, while supporting the general use of switch, do not support default fall-through like C does. To be fair, this is one of the most contreversial features in C (culminating in Duff's Device), but arguments over potential abuse of a feature never stopped Perl before. So it's no small irony that even Java supports a more C-ish switch statment then Perl does. ---- Note: All code is untested, unless otherwise stated | [reply] [d/l] [select] |
|
Re: Switch statement in Perl?
by BrowserUk (Patriarch) on Sep 29, 2003 at 16:31 UTC | |
You might like to look at the thread at Simple Switch statement for a recent discussion of an interesting variation on the theme of self-implemented switch statements. This, in a slightly modifed as shown at Re: Simple Switch statement, is now my current favorite variation. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller If I understand your problem, I can solve it! Of course, the same can be said for you. | [reply] |