henzcoop has asked for the wisdom of the Perl Monks concerning the following question:
Okay, a newbie here. I'm trying to do a very simple little "secret code" script that prompts the user for a message, then substitutes the letters based on a randomly generated alphabet, made fresh each time. The problem comes when I try to drop the $string into the tr// operation. From searching around forums and looking at the manual, I think it has something to do with eval() being needed to interpolate the $string. Greatly appreciate any perls of wisdom! Many thx. Jamie
#!/usr/bin/perl # transliterate.pl 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// my $code = eval($message =~ tr/abcdefghijklmnopqrstuvwxyz/$string/); print "Here is your encrypted message: $code\n"; # As is, $code returns the number of letters in the message.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: secret code, transliteration prob
by Athanasius (Archbishop) on Apr 12, 2016 at 14:13 UTC | |
by henzcoop (Novice) on Apr 12, 2016 at 16:19 UTC | |
|
Re: secret code, transliteration prob
by BrowserUk (Patriarch) on Apr 12, 2016 at 14:17 UTC | |
by henzcoop (Novice) on Apr 12, 2016 at 16:22 UTC | |
by BrowserUk (Patriarch) on Apr 12, 2016 at 17:33 UTC | |
by GotToBTru (Prior) on Apr 13, 2016 at 12:09 UTC | |
|
Re: secret code, transliteration prob
by NetWallah (Canon) on Apr 12, 2016 at 17:43 UTC | |
|
Re: secret code, transliteration prob
by stevieb (Canon) on Apr 12, 2016 at 14:15 UTC | |
by henzcoop (Novice) on Apr 12, 2016 at 16:26 UTC |