From: Robert Klemme Date: 2006-10-21T07:25:07+09:00 Subject: Re: multi dim array? Gijs Nijholt wrote: > Hi (again), > > I have a multi-dimensional array.. walking through an incoming string > like this: > > def translate(msg) > some_alphabet = [ ["a","A"], ["b","B"], ["c","C"], ["d","D"] ] > msg.split(//).inject("") { |result, char| > result += some_alphabet[2][1] > } > end > > This replaces everything in msg with a "C", as it's the third one in the > array. > But I'm looking for a way to replace not by integer/index but by char, > like: > > some_alphabet["c"][1] > > I hope you know what I mean.. I looked at at() and find() and scan() but > neither seems to fit this task? Is this what you want? irb(main):001:0> "message".tr 'abc', 'CBA' => "messCge" Or are you looking for an even more general mapping mechanism? In that case you could use a Hash irb(main):002:0> r={"a"=>"B","e"=>"X"} => {"a"=>"B", "e"=>"X"} irb(main):003:0> "message".gsub(/./) {|k| r[k]||k} => "mXssBgX" Kind regards robert