Gossamer Forum
Home : General : Perl Programming :

alphabet

Quote Reply
alphabet
Hey,
I was wondering if anyone knew how to take a string with plain characters like
my $string = "gossamer";
and get the number of the place of each character and split them with a -. So like a is 0, 1 is 2 and so it would eventually turn it into 6-14-18-18 and so on.
And then something to change the numbers back to the letters. I'd need either this or some good alphanumeric encoding/decoding code that can make the value shorter. Thanks.
Lavon Russell
LookHard Mods
lavon@lh.links247.net

Last edited by:

Bmxer: Nov 21, 2001, 9:25 AM
Post deleted by PaulW In reply to

Last edited by:

PaulW: Nov 21, 2001, 9:52 AM
Quote Reply
Re: [Bmxer] alphabet In reply to
well this will turn letters to numbers....
Code:
%alpha = qw(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10 l 11 m 12 n 13
o 14 p 15 q 16 r 17 s 18 t 19 u 20 v 21 w 22 x 23 y 24 z 25);
$foo = "gossamer";
$alpha_foo = join "-" , map { $alpha{ (lc $_) } } split //, $foo;
and this will turn it back into letters
Code:
%numer = qw(0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 k 11 l 12 m 13 n
14 o 15 p 16 q 17 r 18 s 19 t 20 u 21 v 22 w 23 x 24 y 25 z);
$numer_foo = join "", map { $numer{ $_ } } split /-/, $alpha_foo;

--Philip
Links 2.0 moderator
Quote Reply
Re: [PaulW] alphabet In reply to
basically encrypt/decrypt. And i have code that will encode and decode alphanumerically and take a string maybe 10 characters long and make it into 2. I want the encoded thing short b/c its going in a cookie. But the thing i'm trying to encode doesn't have any numbers associated with it so I can't run it through the routine. So i figure if i could get the number of the letters, then run it throught the routine it will be short. Then the decode will turn it back to the bunch of numbers and then the reverse from numbers to letters would turn it back. I tried Base64 but that makes it way longer than it should be.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [ThatPerson1024] alphabet In reply to
Nah nah nah nah nah:

my $str = 'gossamer';

my $numbers = join '-', map(ord, split //, $str);

my $letters = join '', map(chr, split/-/, $numbers);


I doesn't do a = 1, b = 2 etc, but I think Bmxer said that wasn't essential above

Last edited by:

PaulW: Nov 21, 2001, 10:17 AM
Quote Reply
Re: [PaulW] alphabet In reply to
yeah, except he said he wanted a = 0, b = 1, ect. If he had said ordinals I would have done that Tongue

--Philip
Links 2.0 moderator
Quote Reply
Re: [ThatPerson1024] alphabet In reply to
>>yeah, except he said he wanted a = 0, b = 1, ect.<<

Yep but he just mentioned it wasn't essential so I decided to make that Tongue
Quote Reply
Re: [PaulW] alphabet In reply to
it doesnt really matter to me.
Drew,
yours worked but now i realize its not that easy.
I guess the encoding routine only likes numbers because it keeps printing I. I think its the -.
This worked in changing
Society/Organizations
to
18:14:2:8:4:19:24::14:17:6:0:13:8:25:0:19:8:14:13:18::
but when i try to change it back it's just
societyorganization
I haven't tried yours yet Paul. The outcome of the bad encoding will probably still be the same but I don't see any way of changing the delimiter into anything that could run through the routine. oh, heres the encoding thing.

sub encode {
# --------------------------------------------------------
# Encodes number into a alphanumeric smaller value.
#
my @e = qw!0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z!;

my $in = shift;
my ($out);

while ($in >= ($#e+1)) {
$out = $e[$in % ($#e+1)] . $out;
$in = int $in/($#e+1);
}
$out = $e[$in] . $out;
return $out;
}
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] alphabet In reply to
Oh yeah.... it would be better to use ordinals like Paul suggested if you want upper and lower case, as well as special characters.

--Philip
Links 2.0 moderator
Quote Reply
Re: [ThatPerson1024] alphabet In reply to
yeah, it does work good. I saw that i need to split it with a letter, not a number or character. Characters only make it screw up, and numbers as delimiters get multiplied. Anyway, now the only problem is with this decoding and encoding.
Lavon Russell
LookHard Mods
lavon@lh.links247.net