in reply to simple letter substitution according to hash

$_ = 'Hello World'; my %h = ( e => 'b', l => 'd', ); my $f = join '', keys %h; my $t = join '', values %h; eval "y/$f/$t/"; print __OUTPUT__ Hbddo Wordd
A better choice is to use
$_ = 'Hello World'; my %h = ( e => 'b', l => 'd', ); my $k = join '', keys %h; # Sure, I mean '' Thx@bazar s/([$k])/$h{$1} || $1/eg; print
Boris

Replies are listed 'Best First'.
Re^2: simple letter substitution according to hash
by blazar (Canon) on May 16, 2006 at 14:43 UTC
    $_ = 'Hello World'; my %h = ( e => 'b', l => 'd', ); my $k = join '|', keys %h; s/([$k])/$h{$1} || $1/eg; print

    I think you want either ($k) or my $k = join '', keys %h; in the latter case one would probably also want to check that the keys are actually single letters. In both cases || $1 wouldn't be necessary any more, since the match would necessarily be one of the keys.