sub key { $kp= shift; $caesar= shift; #version 1 - this works, the starting point # foreach (split(//, $kp),'a'..'z') { # if (not exists $p2C{$_}) { # $p2C{$_}=chr(ord('A')+(($a++)+$caesar)%26); # } # } #version 2 - this doesn't work - because autoincrement $a++ gets off when the key already exists! foreach (split(//, $kp),'a'..'z') { $p2C{( exists $p2C{$_} ? '#' : $_)} = chr(ord('A')+(($a++)+$caesar)%26); } %C2p = map {$p2C{$_} => $_} (keys %p2C); $p2C{' '}=' '; $C2p{' '}=' '; } sub printKeys { my $href = shift; print "\nCT: "; print(($_).' ') foreach (sort keys %{$href}); print "\npt: "; print(($$href{$_}).' ') foreach (sort keys %{$href}); print "\n"; } key ('thequickbrownfoxjumpsoverthelazydogs',10); printKeys(\%p2C); printKeys(\%C2p);