[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

multi dim array?

Gijs Nijholt

10/20/2006 9:52:00 PM

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

--
Posted via http://www.ruby-....

4 Answers

Wilson Bilkovich

10/20/2006 9:59:00 PM

0

On 10/20/06, Gijs Nijholt <gijs.nijholt@gmail.com> 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?
>

Does:
some_alphabet.detect {|e| e == "c"}[1]
..do what you're looking for?

Gijs Nijholt

10/20/2006 10:15:00 PM

0

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 Bilkovich wrote:
> On 10/20/06, Gijs Nijholt <gijs.nijholt@gmail.com> wrote:
>> end
>>
> Does:
> some_alphabet.detect {|e| e == "c"}[1]
> ..do what you're looking for?


--
Posted via http://www.ruby-....

Gijs Nijholt

10/20/2006 10:20:00 PM

0

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

--
Posted via http://www.ruby-....

Robert Klemme

10/20/2006 10:20:00 PM

0

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