Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
You may be right about the presentation of that example. I arrived at the code showing the differences through a bit of a convoluted process. I was playing with the differences in a few directions for myself to decide just what to show. I think the code I posted before shows as much about that process as about the resulting idea for the post. Hopefully your idea presents it more clearly to those of you whose voices are outside my head. ;-)

Here's the new C and Perl code and their output:

Perl code:

#!/usr/local/bin/perl use feature qw{ switch }; use strict; use warnings; sub match { my $i = 0; my $foo = shift; given ( $foo ) { when ( 1 ) { $i++; continue; } when ( 2 ) { $i++; continue; } when ( 3 ) { $i++; continue; } when ( 4 ) { $i++; } } print "$foo: $i\n"; } match 1; match 2; match 3; match 4; match 5;

C code:

#include <stdio.h> #include <stdlib.h> int match ( int foo ) { int i = 0; switch ( foo ) { case 1: i++; case 2: i++; case 3: i++; case 4: i++; break; } printf( "%d: %d\n", foo, i ); } int main ( int argc, const char **argv ) { match( 1 ); match( 2 ); match( 3 ); match( 4 ); match( 5 ); exit( 0 ); }

And here's the new output for the Perl:

1: 1 2: 1 3: 1 4: 1 5: 0
And the C:
1: 4 2: 3 3: 2 4: 1 5: 0

In reply to Re^2: Perl 5.10 given/when tricks and caveats by mr_mischief
in thread Perl 5.10 given/when tricks and caveats by mr_mischief

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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (14)
    As of 2015-04-27 17:18 GMT
    Find Nodes?
      Voting Booth?

      Who makes your decisions?

      Results (508 votes), past polls