You are not logged in.

#1 2010-12-09 16:30:01

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

(SOLVED)Perl.... shifting ascii values?

I was curious about Perl today so I started reading a tutorial and before you knew it I had pretty much wrote a small Caeser Cipher program. The only thing I can't figure out is how to shift the ascii characters in the array.

I tried to do it "C" style by adding +1 to the character array index for a +1 shift ("a" +1 = "b"), but apparently Perl doesn't like it, or I didn't use the correct syntax.

Is there any way to do this or am I going to have to use another method? I don't really need to do this program or anything, but trying to get some of the quirks down, smile

I really like what I have seen so far, really quick to pick up and feels very powerful.

Last edited by Google (2010-12-09 17:38:05)

Offline

#2 2010-12-09 17:16:05

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: (SOLVED)Perl.... shifting ascii values?

There's probably a better way (i.e.  a method that involves less typing on your part) but you could implement a hash that reads the input string 1 character at a time and outputs the corrected letter, and for any other non shifted input (whitespace and punctuations) just return themselves as values.

example:

my %cypher = (
       "a" => "c",
       "b" => "d",
       "c" => "e",
...
...
       "y" => "a",
       "z" => "b",
       " " => " ",
       "." => ".");

Last edited by Cyrusm (2010-12-09 17:18:56)


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#3 2010-12-09 17:22:50

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: (SOLVED)Perl.... shifting ascii values?

Interesting, I think this sounds like the easy way to go about something that was static. But, what about a shift that isn't static? If I enter 2 during one runtime and 5 the next I would need 26 hash tables?

Offline

#4 2010-12-09 17:35:51

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: (SOLVED)Perl.... shifting ascii values?

A character can be converted to an integer with ord(), and converted back with chr(). Use modulus to figure out the wraparound.

Offline

#5 2010-12-09 17:37:51

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: (SOLVED)Perl.... shifting ascii values?

I didn't read about those yet, thanks~ very interesting language!

Offline

Board footer

Powered by FluxBB