# Multi dim array?

**URL:** <https://www.ruby-forum.com/t/multi-dim-array/73065>\
**Category:** Ruby\
**Created:** [October 20, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/multi-dim-array/73065 "2006-10-20T23:51:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 20, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/multi-dim-array/73065/1 "2006-10-20T23:51:32Z")

</div>

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?

Thanks!

Gijs

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 20, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/multi-dim-array/73065/2 "2006-10-20T23:59:45Z")

</div>

On 10/20/06, Gijs N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

Does:  
some\_alphabet.detect {|e| e == “c”}[1]  
…do what you’re looking for?

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 21, 2006, 12:14am UTC](https://www.ruby-forum.com/t/multi-dim-array/73065/3 "2006-10-21T00:14:45Z")

</div>

hm, tried several things with detect() and they all gave a nil error  
when least expected error…  
so I thought maybe I need to switch to Hash instead of Array?

excuse me for posting the entire function, but this is what I’ve made of  
it:

def translate\_to\_braille(msg)  
#braille\_alphabet = [[“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”,“â µ”], [" “, " “] ]  
braille\_alphabet = { “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”=\>“â µ”, " “=\>” " }  
#msg.split(//).inject(””) { |result, char| result +=  
braille\_alphabet.first }  
msg.split(//).inject(“”) { |result, char|  
result += char + " " + braille\_alphabet[“r”]  
}  
end

the braille\_alphabet[“r”] nicely gives it’s value, but when I replace it  
with braille\_alphabet[char] Ruby says:  
can’t convert nil into String

why?  
char is not nil, because when I print it, it contains a character…

Wilson B. wrote:

> On 10/20/06, Gijs N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > end
> 
> Does:  
> some\_alphabet.detect {|e| e == “c”}[1]  
> …do what you’re looking for?

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 21, 2006, 12:19am UTC](https://www.ruby-forum.com/t/multi-dim-array/73065/4 "2006-10-21T00:19:41Z")

</div>

nevermind, it does work but something else was throwing nil errors…  
sorry!

---

<div class="post-metadata">

**Author:** ![gijs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gijs](https://www.ruby-forum.com/u/gijs)\
**Post date:** [October 21, 2006, 12:25am UTC](https://www.ruby-forum.com/t/multi-dim-array/73065/5 "2006-10-21T00:25:50Z")

</div>

Gijs N. wrote:

> 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
```
