this is not exactly a one-liner, and i don't think it's possible to do it "within one tr", but once you have the translation hash table, a lot of possibilities open up.

use warnings; use strict; sub make_translator { my ($from, $to, ) = @_; # this check may not be needed. # see tr/// docs for effect of unequal strings. die "from `$from' and to `$to' string lengths differ" if length($from) != length($to); return eval "sub { \$_[0] =~ tr{$from}{$to} }"; } my %trans = ( # translation mappings for target string offsets # at this # do this # offset, # translation. # 1 => make_translator ('abc' => 'XYZ'), 1 => sub { $_[0] =~ tr{abc} {XYZ} }, 3 => make_translator ('def' => 'PQR'), 99 => make_translator ('abc' => '987'), 0 => make_translator ('stu' => '*%&'), ); my %test = ( 'kaldm' => 'kXlPm', 'hbiej' => 'hYiQj', 'sytf' => '*ytR', ); # # works - both ST and for-loop variations # for my $word (keys %test) { # # my @chars = split '', $word; # # # map { $trans{$_}->($chars[$_]) } # 2. xlate char at offset. # # grep { $_ < @chars } # 1. that are in word... # # keys %trans; # 0. for all offsets... # # # for all offsets within the word... # for (grep { $_ < @chars } keys %trans) { # $trans{$_}->($chars[$_]); # translate char at offset. # } # # my $translated_word = join '', @chars; # # printf "%-5s -> %s \n", $word, $translated_word; # # print "ERROR: $word badly translated: \n" # . "should be: $test{$word} \n" # . " got: $translated_word \n" # if $translated_word ne $test{$word}; # # } # works - simplest, probably fastest while (my ($word, $translation) = each %test) { my $original = $word; # save copy: word translated in place printf "%-5s -> ", $word; # for all offsets within the word... for my $offset (grep { $_ < length $word } keys %trans) { # translate char in place: 3-arg substr() returns lvalue. # could translate groups of more than one character if # the width of the group could be defined for each # translation. $trans{$offset}->(substr $word, $offset, 1); } print qq($word \n); print "ERROR: $original badly translated: \n" . "should be: $translation \n" . " got: $word \n" if $word ne $translation; }

In reply to Re: tr at a given pos by Anonymous Monk
in thread tr at a given pos by FFRANK

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.