Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: encryption

by blogan (Monk)
on Nov 07, 2000 at 04:44 UTC ( [id://40297]=note: print w/replies, xml ) Need Help??


in reply to encryption

Yes, this code is extremely inefficient. If you hand it in for homework, your teach will laugh at you. However, if you're having trouble with the Caesar Cipher, it might help you.
#!/usr/bin/perl $line = "I'z abg fnlvat lbh nfxrq n ubzrjbex dhrfgvba..."; for ($i = 0; $i < length $line ; $i++) { $char = substr $line, $i, 1; if ($char eq "a") { $char = "n"; } elsif ($char eq "b") { $char = "o"; } elsif ($char eq "c") { $char = "p"; } elsif ($char eq "d") { $char = "q"; } elsif ($char eq "e") { $char = "r"; } elsif ($char eq "f") { $char = "s"; } elsif ($char eq "g") { $char = "t"; } elsif ($char eq "h") { $char = "u"; } elsif ($char eq "i") { $char = "v"; } elsif ($char eq "j") { $char = "w"; } elsif ($char eq "k") { $char = "x"; } elsif ($char eq "l") { $char = "y"; } elsif ($char eq "m") { $char = "z"; } elsif ($char eq "n") { $char = "a"; } elsif ($char eq "o") { $char = "b"; } elsif ($char eq "p") { $char = "c"; } elsif ($char eq "q") { $char = "d"; } elsif ($char eq "r") { $char = "e"; } elsif ($char eq "s") { $char = "f"; } elsif ($char eq "t") { $char = "g"; } elsif ($char eq "u") { $char = "h"; } elsif ($char eq "v") { $char = "i"; } elsif ($char eq "w") { $char = "j"; } elsif ($char eq "x") { $char = "k"; } elsif ($char eq "y") { $char = "l"; } elsif ($char eq "z") { $char = "m"; } $decoded .= $char; } print $decoded, "\n";

Replies are listed 'Best First'.
RE: Re: encryption
by Adam (Vicar) on Nov 07, 2000 at 05:09 UTC
    #!/usr/bin/perl -w use strict; # $_ = "V'z abg fnlvat lbh nfxrq n ubzrjbex dhrfgvba..."; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; my %ceaser; @ceaser{'A'..'Z','a'..'z',' ','.',"'"} = ('N'..'Z','A'..'M', 'n'..'z', 'a'..'m', ' ','.',"'"); my $decoded; $decoded .= $ceaser{$_} for split //; print $decoded, "\n";
      Slightly more compact:
      #!/usr/bin/perl -w use strict; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; tr/A-Za-z/N-ZA-Mn-za-m/; print;
        and a byte more compact: #!/usr/bin/perl -w use strict; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; y/A-Za-z/N-ZA-Mn-za-m/; print;
        thank you for doing my homework now im sure to gat an 'A+"
      Minor change, but now you don't have to define what happens for characters that shouldn't be "decrypted".
      #!/usr/bin/perl -w use strict; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; my %caesar; @caesar{'A'..'Z','a'..'z'} = ('N'..'Z','A'..'M', 'n'..'z', 'a'..'m'); my $decoded; $decoded .= $caesar{$_} ? $caesar{$_} : $_ for split //; print $decoded, "\n";
        #!/usr/bin/perl -w use strict; my $line = "Lbh pbhyq rira hfr n gvrq inevnoyr... 3-)"; my %caesar; @caesar{'A'..'Z','a'..'z',0..9} = ('N'..'Z','A'..'M', 'n'..'z', 'a'..'m', 5..9,0..4 ); sub TIESCALAR{bless[pop]} sub FETCH{exists $caesar{$_}?$caesar{$_}:$_} tie my $chars, '', ''; print $chars for split //, $line;
        Although I like the use of transpose more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://40297]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-11-11 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (39 votes). Check out past polls.