From: "Peña, Botp" Date: 2008-05-27T11:54:48+09:00 Subject: Re: make hashtable out of 2 strings From: notnorwegian@yahoo.se [mailto:notnorwegian@yahoo.se] # i have 2 strings, i want to take for each char in string1 the same # indexed char in string2 and make them into a key->value pair. # how can i iterate a string? i'm currently playing w ruby1.9 now, so pls bear w me ;) irb(main):001:0> s1 = "asdf" => "asdf" irb(main):002:0> s2 = "bnmd" => "bnmd" irb(main):004:0> pairs = s1.each_char.zip s2.each_char => # i like the flexibility of enums, so you can do irb(main):005:0> pairs.each {|x| p x} ["a", "b"] ["s", "n"] ["d", "m"] ["f", "d"] => nil irb(main):006:0> pairs.map{|x|x} => [["a", "b"], ["s", "n"], ["d", "m"], ["f", "d"]] irb(main):014:0> pairs.inject({}){|h,(k,v)| h[k]=v; h} => {"a"=>"b", "s"=>"n", "d"=>"m", "f"=>"d"} irb(main):029:0> pairs.map(&:reverse) => [["b", "a"], ["n", "s"], ["m", "d"], ["d", "f"]] irb(main):032:0> pairs.map(&:first).join => "asdf" kind regards -botp