Sorry for giving you code that didn't directly work earlier - please reply directly to my posts if you expect me to read them, as only then I get a notification.

The correct code for creating the list of keys and the list of where they should be mapped to is the following. Previously, I had thought that the range operator .. would also work for characters outside the printable range, but it works its magic only for strings between A and Z...

First, always keep use strict; enabled - it will help you catch spelling errors and other hard to track errors. use strict; is your friend!

The initialization of the validchars map can be done as follows:

if ( $act eq '-a' ) { %validchars = map { $_ => uc $_ } map { pack "C", $_ } ( 0 .. 255 ); } elsif ( $act eq '-c' ) { %validchars = map { $_ => $_ } map { pack "C", $_ } ( ord('A').. ord('Z'), ord('a').. +ord('z') ); };

The sequence of map calls is converting the list at the end of that sequence in two steps. First, the list is converted from the numbers to the characters represented by these numbers, and then, in the first map, every character is converted to a pair mapping the character onto the character that is to be counted.


In reply to Re^2: Switch statement? by Corion
in thread Switch statement? by crep

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.
  • 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, details, 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, summary, 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:
    & &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.