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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Growing up with an actuary as a Father meant lots of mathematics while growing up. OK, I had an aptitude for it, that helped. There were a lot of number and word games, and one of them was called fizz-buzz. You could play it anywhere, around the dinner table, while out for a walk in the Morgan Arboretum, or on a long car ride to or from Cape Cod. It goes like this:

The players recite the numbers in ascending order, starting at 1, except when you get to a multiple of 3, you say 'fizz', and when you get to a multiple of 5 you say 'buzz'. The number before 16, is, of course, fizz-buzz. To make things more interesting and a bit more challenging, my Father added 'sausage' when the number was a multiple of 7, thus the trifecta was fizz-buzz-sausage after 104 turns.

This morning I wanted to try out Perl's new switch statement ..

#!/usr/bin/perl510 use feature 'switch'; use feature 'say'; { for ( 1..40 ) { my @what; given ( $_ ) { when ( $_ % 3 == 0 ) { push ( @what, 'fizz' ); } when ( $_ % 5 == 0 ) { push ( @what, 'buzz' ); } when ( $_ % 7 == 0 ) { push ( @what, 'sausage' ); } } say join(' ',$_,@what); } }

but found it didn't produce the output I expected. Unlike C's switch statement, once a condition is met, the entire given construct is finished.

1 2 3 fizz 4 5 buzz 6 fizz 7 sasusage 8 9 fizz 10 buzz 11 12 fizz 13 14 sasusage 15 fizz 16 17 18 fizz 19 20 buzz 21 fizz 22 23 24 fizz 25 buzz 26 27 fizz 28 sasusage 29 30 fizz 31 32 33 fizz 34 35 buzz 36 fizz 37 38 39 fizz 40 buzz

That's OK -- now I know. Oh, and to get this to run, I had to run Perl 5.10 from the directory I built it in, and include -Ilib on the command line (thanks mauke). I made the mistake of upgrading the system Perl one time, and that's a mistake I won't ever make again.

Alex / talexb / Toronto

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


In reply to Perl 5.10: switch statement demo by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (10)
    As of 2014-12-19 22:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (94 votes), past polls