Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Perl 5.10: switch statement demo

by Limbic~Region (Chancellor)
on Dec 19, 2007 at 20:24 UTC ( #657967=note: print w/replies, xml ) Need Help??

in reply to Perl 5.10: switch statement demo

Golf Challenge: FizzBuzz may be of interest to you. Also, take a look at perlsyn. While there is an implicit break in every when clause, you can get around this by using an explicit continue. A few other notes, you could use feature qw/say switch/;. There is more discussion of perl 5.10 doodads at Bring Out Your New Perl Code. For example:
#!/usr/bin/perl use 5.010; use strict; use warnings; for (1 .. 105) { my $what = ''; given ($_) { when (not $_ % 3) { $what .= ' fizz'; continue } when (not $_ % 5) { $what .= ' buzz'; continue } when (not $_ % 7) { $what .= ' sausage' } } say "$_$what"; }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Perl 5.10: switch statement demo
by ikegami (Pope) on Dec 19, 2007 at 22:32 UTC

    I wonder why you switched from "when the remainder is zero" to "when not the remainder". (Reminds me of the baby in Dinosaurs: "Not the Momma! Not the Momma!")

    Update: Nevermind, I guess it can be read "when no remainder". I guess I don't think of a remainder of zero as there being no remainder, so it looks weird to me.

      The modulo construct can also be thought of as 'divisible by' -- so $foo % 3 checks to see if $foo is divisible by 3. Or at least that's the way my brain works.

      Alex / talexb / Toronto

      "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

        That's backwards. $foo % 3 checks if $foo *isn't* divisible by 3. not $foo % 3 would deceptively check if is divisible by 3.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://657967]
[karlgoethebier]: Rollmops == German Sushi
[erix]: Haxen (sushi?)
[Discipulus]: yes choroba is one of his puns.. ;=)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2017-11-21 11:25 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (297 votes). Check out past polls.