ekofoed
6/14/2006 8:25:00 AM
How about
nr = (1..26).to_a
let=('a'..'z').to_a
h = {}
let.each {|letter| h[letter] = nr.shift}
irb(main):013:0> h
=> {"v"=>22, "k"=>11, "w"=>23, "l"=>12, "a"=>1, "x"=>24, "m"=>13,
"b"=>2, "y"=>25, "n"=>14, "c"=>3, "z"=>26, "o"=>15, "d"=>4, "p"=>16,
"e"=>5, "q"=>17, "f"=>6, "r"=>18, "g"=>7, "s"=>19, "h"=>8, "t"=>20,
"i"=>9, "u"=>21, "j"=>10}
regards
-erik
Drew wrote:
> I'm trying to take two ranges (namely, (1..26) and ('A'..'Z')) and
> merge them into a new hash such that A=>1, B=>2, etc. I know I could
> easily do this with a loop but I want to use an iterator and I'm not
> quite sure how to approach it. I know this isn't right but it's the
> best I ccould come up with:
>
> letters = ('A'..'Z')
> numbers = (1..26)
> letter_to_number = {}
> numbers.each, letters.each {|num, let| letter_to_number[let => num]}
>
> If I use that, I get "1solitaire.rb:32: parse error, unexpected '\n',
> expecting tCOLON2 or '[' or '.'"
> (line 32 is the last line in the code above)
>
> I'm rather new to Ruby but so far I'm very impressed with it as well as
> the community - thanks for any pointers :)