We don't bite newbies here... much PerlMonks

Comment on

 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!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [choroba]: Merkine? [erix]: Nashorn is Diceros not monoceros [choroba]: Seefeld in Tirol? [shmem]: this erix notes the cloven hoof

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-03-30 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (362 votes). Check out past polls.