Hi,
for a very simplistic encryption program I want to build a
hash, which should be dynamically build (not hardcoded).
The input of the method which should do the job will be a range of
any meaningful input, say: parameter 1: A and parameter 2: Z
Then the method should create a Hash with that mapping
“A” -> “Z”
“B” -> “G”
“C” -> “T”
…
or in other words:
The key should follow the given range, and the values should
represent all values of that range but in random order.
For arrays I learned a nice trick from this list:
encmap=(A..Z).to_a.sort{rand(2)}
but…how can I accomplish similiar for a hash ???
And: I sthere a simple way to store a hash in binary format and later
load that hash again into memory ?
Nice weekend!
mcc
Maybe: encmap=Hash[*(‘A’…‘Z’).to_a.sort{rand(2)}]
and Marshal.dump and Marshal.load for storing.
j`ey
http://www.eachmapinject.com
From: Joey [email protected]
Subject: Re: Smoking hash-ish: Letter mapping…
Date: Sun, 6 Aug 2006 03:55:14 +0900
Looks good! May be I am too newbie here, but…
When I do this in irb and look at encmap, both - key and value -
seem to be randomnized…
(and what means the “*” right at the start of the expression?)
But this is not really an issue as long there is no identical mapping
like “A”->“A”.
Ah, by the way: With an even number of items/mapping in such an
Hash…there must be zero or an even number of identical mappings like
described above. Is this logical correct or nonsense…?
On Aug 5, 2006, at 3:07 PM, Meino Christian C. wrote:
Oh…there is another issue:
With encmap=Hash[*(‘A’…‘Z’).to_a.sort{rand(2)}] I only will get 13
instead of 26 mappings! How can I fix /that/ ?
Keep encoding! 
mcc
Actually its generally considered better to do sort_by{rand} that sort
{rand(2)}
If you only want to randomize key or value you can do
first = (“A”…“Z”).to_a
second = (“A”…“Z”).sort_by{rand}
encmap = Hash[ *first.zip(second).flatten ]
Oh…there is another issue:
With encmap=Hash[*(‘A’…‘Z’).to_a.sort{rand(2)}] I only will get 13
instead of 26 mappings! How can I fix /that/ ?
Keep encoding! 
mcc