OK, I'm trying things. Here's what I've got.
This first one is the nominal - it works and accomplishes what I want. I've decluttered from above so I'm only dealing with one hash and not using a temporary variable for the big, modular math. I've added a nice map statement to turn %p2C inside out, becoming %C2p.
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); } } %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);
This produces output thus:
C:\chas_sandbox>foreachasmap.pl CT: 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 pt: F S Q I M X J L P Z R E A W U B N T C K O D V Y H G CT: 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 pt: m p s v l a z y d g t h e q u i c k b r o w n f x j C:\chas_sandbox>
Next, I've turned the if statement into a ( ? : ) operator. In version 1 I was looking for if (not exists . . .) so I needed something to do if it did exists and assigned to a hash key that I knew I wouldn't use, '#'. I've spotted my bug here - the autoincrement of $a happens every time now instead of only if (not exists . . .). Any suggestions for that or am I a victim of my own ambition?
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 w +hen the key already exists! foreach (split(//, $kp),'a'..'z') { $p2C{( exists $p2C{$_} ? '#' : $_)} = chr(ord('A')+(($a++)+$ca +esar)%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);
Here's the second output.
C:\chas_sandbox>foreachasmap.pl CT: # 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 pt: T N S Q Q M X S L P A R M C W U D N T E K O G V Z P O CT: A C D E G K L M N O P Q R S T U V W X Z pt: j m p s v t h l q z i c k b # o w n f x
The last version here would be what I'm really after. I'm just moving things around according to the doc for map:
is just a funny way to write%hash = map { getkey($_) => $_ } @array;%hash = (); foreach $_ (@array) { $hash{getkey($_)} = $_; }
I have a foreach, a list and I'm mapping a key to a value in a hash, so it seems it should be no more complicated than this. Here's the code:
sub key { $kp= shift; $caesar= shift; #version 2 - this doesn't work - because autoincrement $a++ gets off w +hen the key already exists! # foreach (split(//, $kp),'a'..'z') { # $p2C{( exists $p2C{$_} ? '#' : $_)} = chr(ord('A')+(($a++)+$ +caesar)%26); # } #version 3 - this doesn't work - why? %p2C = map {( exists $p2C{$_} ? '#' : $_ )=> chr(ord('A')+(($a++)+ +$caesar)%26) } (split(//, $kp),'a'..'z'); %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);
I don't even get the same output as in version 2:
C:\chas_sandbox>foreachasmap.pl CT: 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 pt: U V W X Y Z A B C D E F G H I J K L M N O P Q R S T CT: 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 pt: g h i j k l m n o p q r s t u v w x y z a b c d e f C:\chas_sandbox>
I know this may all seem academic since it's working in version 1, but, well, it's academic and I'd like to learn something.
In reply to Re^2: adapting foreach to map (why?)
by goibhniu
in thread adapting foreach to map
by goibhniu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |