in reply to secret code, transliteration prob

Hello henzcoop,

You’re nearly there. But (1) eval returns the value of the last expression evaluated; but you don’t want the number of transliterations, you want the transliterated string, which is $message. (2) Within an eval, a variable such as $message will itself be interpolated. But you want to evaluate the expression $message =~ ..., so you need to escape the $ sigil to prevent interpolation of the $message variable into the string to be evaluated.

#! perl use strict; use warnings; print "Please enter your message to be encrypted.\n"; my $message = <STDIN>; chomp($message); # generate randomized alphabet my @chars = ("A".."Z", "a".."z"); my $string; $string .= $chars[rand @chars] for 1..26; # I want to drop the random alphabet in $string into the replace-with +slot of tr// eval "\$message =~ tr/a-z/$string/;"; print "Here is your encrypted message: $message\n";

Alternatively, you can add an /r modifier to the transliteration to get it to return the transliterated value:

my $code = eval "\$message =~ tr/a-z/$string/r;"; print "Here is your encrypted message: $code\n";

See the entry for tr/SEARCHLIST/REPLACEMENTLIST/cdsr under perlop#Quote-Like-Operators.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: secret code, transliteration prob
by henzcoop (Novice) on Apr 12, 2016 at 16:19 UTC

    Hey, look at that--it works! Thanks for walking me through this, Athanasius. You're a natural teacher.