You might want to consider something like:
my @abc = ('a'..'z');
my %abc = map { ($abc[$_],$_) } (0..25);
my @key = map { $abc{ lc() } } $key =~ /([a-z])/ig;
To use it, loop through the characters in your message any way you like. The following assumes you have the current letter in
$c , that
$i holds your position in
@key , and that the result will be in
$result . . .
my $new_c;
if ($c =~ /[a-z]/i) {
$new_c = $abc[($abc{lc($c)} + $key[$i]) % 26];
$new_c = uc($new_c) if ($c =~ /[A-Z]/);
$result .= $new_c;
$i = ($i + 1) % @key;
} else {
$result .= $c;
}
You could use the same code above for decoding if you changed
@key to hold negative numbers instead of positives.
-sauoq
"My two cents aren't worth a dime.";