Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

char. VS char.

by Summers_Azin_pr0n (Initiate)
on Aug 30, 2001 at 04:44 UTC ( #108966=perlquestion: print w/replies, xml ) Need Help??

Summers_Azin_pr0n has asked for the wisdom of the Perl Monks concerning the following question:

OK, my idea is to try to make a program that I could type in a string and out would come a systamaticly diffrent string. Kind of like it is being incoded. Basicly I want to say for each char. EXAMPLE---> ( A=W , B=R, C=T,... and the string ABC would come out WRT.) I am thinking an array of some sort but I keep geting stumped. ALL AND ANY HELP APRECEATED! (Im a good speller)

Replies are listed 'Best First'.
Re: char. VS char.
by I0 (Priest) on Aug 30, 2001 at 04:49 UTC
    $example = 'ABC'; $example =~ tr/ABC/WRT/;
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
      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);


Re: char. VS char.
by blakem (Monsignor) on Aug 30, 2001 at 04:52 UTC
    How about:
    $string =~ tr/ABC/WRT/;


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.'

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.

    print "I pity the $foo !\n";
      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.
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!
      What, not good enough? Fine, I can golf a mean "double ROT 13", then...


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)];
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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://108966]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2021-01-25 11:08 GMT
Find Nodes?
    Voting Booth?