in reply to simple letter substitution according to hash
Why not use regex substitution?
#!/usr/local/bin/perl use strict; use warnings; my %codewheel = ( 'A' => 'B', 'B' => 'C', 'C' => 'D' ); my $message = 'ABC'; $message =~ s/(\w)/${codewheel{$1}}/g; print "Message: $message\n";
Update: Of course, as pointed out by some of the later responses, this code suppresses any characters that are not accounted-for in your %codewheel hash. For example, with the same translation hash, if your original message was 'ABCXYZ', this code would still translate it as 'BCD' (and produce some warnings). If you don't want that outcome, you'll need to adapt the 'or' (||) logic shown in other responses.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: simple letter substitution according to hash
by blazar (Canon) on May 16, 2006 at 14:37 UTC |