CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Translation Hash

 



cyberfarer
Novice

Dec 11, 2012, 10:38 AM

Post #1 of 6 (1245 views)
Translation Hash Can't Post

Hello,

I have a paragraph of text in a file that I am to translate using a translation hash. I am completely stalled on approach and on using hash keys and values as variables.

The translation hash looks like this:

Code
%tr_hash = ( 
a => l
b => b
c => e
d => z
e => g
f => s
g => d
h => w
i => f
j => h
k => q
l => u
m => o
n => k
o => n
p => p
q => y
r => r
s => j
t => a
u => t
v => c
w => v
x => x
y => m
z => i
);


The message I am to translate looks something like this:

Code
kernndg vlk bgaagr ajlf jwk vnrz. jg zwz wa luu 
(snipped to prevent really long lines)


So basically it is a letter substitution where I replace $v with $k.

My problem is I don't know how to use hash keys and values as variables. For example s/$v/$k

Next, I'm not sure how to even approach it as I think I would have to loop through the message text while looping through the hash keys for the substitution. So I think I would have a while statement for the file and a foreach statement for the hash but everytime I try to code it I can't seem to progress beyond the "hmmmmm: stage.

Thanks for any feedback.


(This post was edited by cyberfarer on Dec 11, 2012, 10:42 AM)


Laurent_R
Veteran / Moderator

Dec 11, 2012, 11:58 AM

Post #2 of 6 (1239 views)
Re: [cyberfarer] Translation Hash [In reply to] Can't Post

You don't need a while statelement to look the the value associated with a key in a hash.

Basically, what you have to do is to read your input file, to split every line of your input into single letters, and, for each letter, look for its translation in the hash, and print out the translation.

As a basic example, taking the first word of your file.


Code
my $input = "kernndg"; 
my @letters = split //, $input;
foreach my $letter (@letters) {
print $tr_hash{$letter};
}


Of course, you'll have to do a bit more (e.g managing new lines, etc.) but that is the basic idea that should put you on the right track.


BillKSmith
Veteran

Dec 11, 2012, 2:27 PM

Post #3 of 6 (1230 views)
Re: [cyberfarer] Translation Hash [In reply to] Can't Post

I think you were on the right track with your regular expression. Use the translation hash in the replacement field.

Code
use strict; 
use warnings;
my %tr_hash = (
a => 'l', b => 'b', c => 'e', d => 'z', e => 'g',
f => 's', g => 'd', h => 'w', i => 'f', j => 'h',
k => 'q', l => 'u', m => 'o', n => 'k', o => 'n',
p => 'p', q => 'y', r => 'r', s => 'j', t => 'a',
u => 't', v => 'c', w => 'v', x => 'x', y => 'm',
z => 'i',
);
my $message = <DATA>;
$message =~ s/([a-z])/$tr_hash{$1}/g;
print $message, "\n";
__DATA__
kernndg vlk bgaagr ajlf jwk vnrz. jg zwz wa luu

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Dec 12, 2012, 4:43 AM

Post #4 of 6 (1218 views)
Re: [BillKSmith] Translation Hash [In reply to] Can't Post

Yes, right, it is simpler to do the substitution in a regular expression.


cyberfarer
Novice

Dec 12, 2012, 6:30 AM

Post #5 of 6 (1216 views)
Re: [BillKSmith] Translation Hash [In reply to] Can't Post

Thank you, Bill and Laurent. I hope you don't mind if I ask a follow-up question. I was trying to do a substitution but I was finding it quite impossible and your solution is so much simpler.

When you use

Code
$message =~ s/([a-z])/$tr_hash{$1}/g;


How does the $1 work?

Thank you.


BillKSmith
Veteran

Dec 12, 2012, 7:17 AM

Post #6 of 6 (1212 views)
Re: [cyberfarer] Translation Hash [In reply to] Can't Post

The purpose of the parenthesis in the regexpr is to "capture" the text matched by pattern between them. The text captured by the first (in this case only) set of parenthesis is available as a special perl variable "$1". The character class between the parenthesis matches a single lower-case character. This is captured into $1 and used as the key into the translation hash in the substitution field. The corresponding hash value then replaces original character. The "/g" at the end tells perl to repeat the process as often as possible.

Note: Although not used here, perl has analogous special variables $2, $3,..... to capture text from the second, third, etc. pair of parenthesis.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives