Re: char. VS char.
by I0 (Priest) on Aug 30, 2001 at 04:49 UTC
|
$example = 'ABC';
$example =~ tr/ABC/WRT/;
| [reply] [d/l] |
Re: char. VS char.
by MZSanford (Curate) on Aug 30, 2001 at 12:43 UTC
|
While tr/// will do the replace, i thought i should comment on the idea of an array being used. If you are keeping track of a one-to-one relationship, a hash is the best solution.
People new to perl often don't use hashes, but a quick example would be :
my %hash = (
A => W,
B => R,
C => T,
);
my $letter = 'A';
print "Letter : $letter\n"; ## prints "Letter : A"
print "Letter : $hash{$letter}\n"; ## prints "Letter : W"
just figured to hit the problem where you are already working on it, rather than going with tr/// and having you work out the complex array solutions for your next project (which i did as a newbie)
can't sleep clowns will eat me
-- MZSanford
| [reply] [d/l] [select] |
|
I agree, hashes seem to be more appropriate for this kind of thing, I've whipped up a generic sub here that seems to be a little more in tone with what the original poster requested:
#!/usr/bin/perl -w
use strict; #always
sub myencode {
my ($sentence,$encodehash)=@_ or die "not enough parameters for enco
+de:@_";
foreach my $key (keys %$encodehash) {
$sentence =~ s!$key!$$encodehash{$key}!g;
}
return $sentence;
}
my %encoding=("A"=>"D","B"=>"E","C"=>"F");
print myencode("ABC",\%encoding);
qw[marcus] | [reply] [d/l] [select] |
Re: char. VS char.
by blakem (Monsignor) on Aug 30, 2001 at 04:52 UTC
|
$string =~ tr/ABC/WRT/;
-Blake
| [reply] [d/l] |
Re: char. VS char.
by mitd (Curate) on Aug 30, 2001 at 05:38 UTC
|
'Holy Enigma Plops'
Try this:
use Term::ReadKey;
ReadMode 3;
$|++;
@box = ( (L .. Z), (A .. K));
print "My secret Code: ",join(',',@box),"\n";
while(1) {
my $key = ReadKey(-1);
$key = uc($key);
last if $key eq 'Q';
print $box[ord($key) - 65];
}
ReadMode 0;
mitd-Made in the Dark
'My favourite colour appears to be grey.' | [reply] [d/l] |
Re: char. VS char.
by Mr.T (Sexton) on Aug 30, 2001 at 05:34 UTC
|
First of all: I know that you are a newbie here at the Monastery, (your XP shows all too well...) and being a newbie, you need to learn some of what we do here, and how things work around here. One of these things you need to learn, is how to correctly title questions. Your question is legitimate, but your title is not very specific in its description. char. VS char. doesn't really give us an idea right off hand what you are asking. (Do char's fight eachother or something?)
Second: Have you even read any Perl-related learning books for beginners? It doesn't seem like it. Check out Learning Perl sometime, it will help you get up and on your feet quickly.
Otherwise, keep at it, and welcome to the monastery.
Mr.T
print "I pity the $foo !\n"; | [reply] |
|
Give the guy a break. Obviously he is new. If he doesn't know how to do what he wants to do (with his code) then how do you expect him to come up with a perfect title? If the title was really that bad then somebody will step in with editorial privs and change it.
| [reply] |
Re: char. VS char.
by Zecho (Hermit) on Aug 30, 2001 at 06:00 UTC
|
Do you mean something like ROT 13? something a little more generic than what has been suggsted before?
Update: added the rest of the question gonna lose my voting privs over this one! :) | [reply] |
|
What, not good enough? Fine, I can golf a mean "double ROT 13", then...
-Blake
| [reply] |
Re: char. VS char.
by Rhandom (Curate) on Aug 30, 2001 at 21:11 UTC
|
YAHP - Yet Another Hash post -- character by character is safer than multiple swaps over the same string.
#!/usr/bin/perl
my $text = "ABCCBAABCCBA";
my $hash = {
A => 'W',
B => 'R',
C => 'T',
};
$text2 = join "", map { $hash->{$_} } split //, $text;
print "[$text]\n";
print "[$text2]\n";
my @a=qw(random brilliant braindead); print $a[rand(@a)]; | [reply] [d/l] |
Re: char. VS char.
by John M. Dlugosz (Monsignor) on Aug 30, 2001 at 23:28 UTC
|
You might also look at Vigenère Cypher for what it looks like if you take this kind of code to its logical conclusion.
—John | [reply] |