bioBob has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to use tr to do a binned replacement of a bunch of characters ASCII 33-126 where I bin the replacements as 33-42, 43-52, 53-62 and >62 and rename them 0, 1, 2, and 3.

I have tried all combinations (but not the right one) to affect this and seem to get at best replacement of all values to 3. Here are a few iterations:

###$quality =~ tr /[!"#$%&'()*][+,-./01234][56789:;<=>][?@ABCDEFGHIJKL +MNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~]/0123/; ###$quality =~ tr /[\!-\*][\+-4][5-\>][\?-\~]/0123/; ###$quality =~ tr/[\x21-\x2a][\x2b-4][5-\x3e][\x3f-\x78]/0 +123/; ###$quality =~ tr/[\x21-\x2a][\x2b-\x34][\x35-\x3e][\x3f-\ +x78]/0123/; ###$quality =~ tr/[\!-\*][\+-4][5-\>][\?-\~]/0123/;

Any suggestions?

Thanks,

Bob

Replies are listed 'Best First'.
Re: using tr
by BrowserUk (Patriarch) on Feb 28, 2012 at 22:13 UTC

    Like this?:

    $s = pack 'C*', 33 .. 126;; print $s;; !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdef +ghijklmnopqrstuvwxyz{|}~ $x = join'', '0'x10, '1'x10, '2'x10, '3'x64;; eval qq[\$s =~ tr[\\x20-\\x7e][$x]];; print $s;; 0000000001111111111222222222233333333333333333333333333333333333333333 +333333333333333333333333

    Or, without the eval (Updated: The last character in the replacement is reused so shortening the construct):

    $s = pack 'C*', 33 .. 126;; print $s;; !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdef +ghijklmnopqrstuvwxyz{|}~ $s =~ tr[\x20-\x7e][0000000000111111111122222222223];; print $s;; 0000000001111111111222222222233333333333333333333333333333333333333333 +333333333333333333333333

    ps. Please find out what <code></code> tags are and use them in future.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: using tr
by AnomalousMonk (Archbishop) on Feb 29, 2012 at 00:07 UTC

    Here are a couple of other approaches. The  s/// operation will be significantly slower than  tr// for very long strings (for some definition of 'very long').

    (And yes, please do edit the OP to use code tags. Please see Markup in the Monastery and Writeup Formatting Tips.)

    >perl -wMstrict -le "my @ranges = ([33..42], [43..52], [53..62], [63..126]); ;; my $chars = join '', map { map chr, @$_ } @ranges; $chars =~ s{\\}'\\\\'xms; $chars =~ s{/}'\/'xms; ;; my $bins = join '', map { $_ x @{$ranges[$_]} } 0 .. $#ranges - 1; $bins .= $#ranges; ;; print qq{'$chars'}; print qq{'$bins'}; ;; eval qq{ sub binned { (my \$t = \$_[0]) =~ y/$chars/$bins/; return \$t; } }; ;; my $test = qq[!\x22 )#+, 3456 =>?@ }~]; print qq{test string: '$test'}; printf qq{tr// method: '%s' \n}, binned($test); ;; ;; my %xlate = map { my $i = $_; map { chr() => $i } @{$ranges[$_]} } 0 .. $#ranges ; ;; $test =~ s{(.)}{ exists $xlate{$1} ? $xlate{$1} : $1 }xmsge; print qq{s/// method: '$test'}; " '!"#$%&'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc +defghijklmnopqrstuvwxyz{|}~' '0000000000111111111122222222223' test string: '!" )#+, 3456 =>?@ }~' tr// method: '00 0011 1122 2233 33' s/// method: '00 0011 1122 2233 33'

    Updates:

    1. Improved example code slightly: removed MS-reactive " (double-quote) characters from source; better printout labeling, formatting.
    2. In the last  s/// statement, the  exists $xlate{$1} ? $xlate{$1} : $1 expression can be replaced by  $xlate{$1} // $1 in 5.10+ for possibly slightly faster execution (not Benchmarked).

Re: using tr
by Eliya (Vicar) on Feb 28, 2012 at 22:16 UTC
    $quality =~ tr/[\!-\*][\+-4][5-\>][\?-\~]/0123/;

    tr doesn't do character classes.  The two strings specify lists of characters (with ranges allowed) that must correspond. For example, to replace A-D with 1 and F-H with 2, you'd say

    tr/A-DF-H/1111222/;