in reply to Switch statement?

Errg... corian, you are amazing.. but I think I've screwed up you nice code in trying to put it in with the old. I get all sorts of errors, particularly one about a bad charactor, 0xff. Take a look:
#!/usr/bin/perl -w #use strict; my $infile = $ARGV[0]; my $act = $ARGV[1]; my $included = "n"; my $lvar =""; my %validchars; if ($#ARGV < 1) { print "Usage: $0 <file> <chars> Where <chars> can be -a, -c, or -r: -a: show all charactors. (ie, spaces, etc.) -c: show only regular charactors, but keep case. (ie, x is diff +erent than X.) -r: show only regular charactors, disregard case. (ie, t and T +are the same.)\n"; exit; } if ( $act eq '-a' ) { %validchars = map { $_ => uc $_ } ( \x00 .. \xff ); } elsif ( $act eq '-c' ) { %validchars = map { $_ => $_ } ( 'A' .. 'Z', 'a'..'z' ); }; my $count = 0; system("cls"); #win system("clear"); #*nix open INPUT, "<$infile" or die "Couldn't open '$infile' : $!"; my $input = do { local $/; <INPUT> }; my %histogram; for my $char (split //, $input) { if (exists $validchars{$_}) { my $target = $validchars{$_}; $count++; $histogram{$target}++}; }; print "Of $count charactors:\n\n"; for (sort keys %histogram) { if ($_ eq "\n") { print "'\\n' occurred $histogram{$_} times. (".(($ +histogram{$_}/$count)*100)."%)\n"} else { print "'$_' occurred $histogram{$_} times. (".(($histogram{$ +_}/$count)*100)."%)\n" } };
Thanks again guys (monks)! -Jack

Replies are listed 'Best First'.
Re^2: Switch statement?
by Corion (Patriarch) on Jul 19, 2004 at 22:06 UTC

    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.

      Hm... well I switched out the statement, and I get several lines of the same error:
      Use of uninitialized value in exists at frequency-new.pl line 41, <INPUT> line 1.
      where line 41 is:
        if (exists $validchars{$_}) {

      Hm.
      Thanks again, -Jack C