how to create a hash from 2 arrays?

T

Tiggerius

keys = ["1", "2", "3", "4", "5"]
vals = ["a", "b", "c", "d", "e", "f", "g"]

I want h = {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"}

right now I do it by:
for i in 0..keys.size-1
h[keys]=vals
end

is there a ruby way, more slick way of doing this? Thanks.
 
S

Simon Strandgaard

keys =3D ["1", "2", "3", "4", "5"]
vals =3D ["a", "b", "c", "d", "e", "f", "g"]

I want h =3D {"1"=3D>"a", "2"=3D>"b", "3"=3D>"c", "4"=3D>"d", "5"=3D>"e"}

right now I do it by:
for i in 0..keys.size-1
h[keys]=3Dvals
end

is there a ruby way, more slick way of doing this? Thanks.



Hash[*keys.zip(vals).flatten]
#=3D> {"1"=3D>"a", "2"=3D>"b", "3"=3D>"c", "4"=3D>"d", "5"=3D>"e"}
 
B

Brian Schröder

keys =3D ["1", "2", "3", "4", "5"]
vals =3D ["a", "b", "c", "d", "e", "f", "g"]

I want h =3D {"1"=3D>"a", "2"=3D>"b", "3"=3D>"c", "4"=3D>"d", "5"=3D>"e"}

right now I do it by:
for i in 0..keys.size-1
h[keys]=3Dvals
end

is there a ruby way, more slick way of doing this? Thanks.


Not as slick as simons version, but never miss a possibility to use inject.

irb(main):001:0> keys =3D ["1", "2", "3", "4", "5"]
=3D> ["1", "2", "3", "4", "5"]

irb(main):002:0> vals =3D ["a", "b", "c", "d", "e", "f", "g"]
=3D> ["a", "b", "c", "d", "e", "f", "g"]

irb(main):003:0> keys.zip(vals).inject({}) { | h, (k, v) | h[k] =3D v; h }
=3D> {"1"=3D>"a", "2"=3D>"b", "3"=3D>"c", "4"=3D>"d", "5"=3D>"e"}

regards,

Brian
 
E

ES

Tiggerius said:
keys = ["1", "2", "3", "4", "5"]
vals = ["a", "b", "c", "d", "e", "f", "g"]

I want h = {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"}

right now I do it by:
for i in 0..keys.size-1
h[keys]=vals
end

is there a ruby way, more slick way of doing this? Thanks.


Array#zip is a good way to get started. It takes two Arrays and
'zips' them together: [1].zip([2]) => [[1, 2]]. #inject can be
used to accumulate data, here we accumulate into the Hash (which
is the initial {} there):

keys.zip(values).inject({}) {|hash, (k, v)| hash[k] = v; hash}

An alternative syntax would leverage the ability of Hash.[] to
take an even number of parameters and construct its keys and
values from that: Hash[1, 2] => {1 => 2}. To create separate
parameters from the contents of an Array, use the splat-operator
(*): *[1, 2] => 1, 2:

Hash[*keys.zip(values).flatten]

This only works when you do not have nested Arrays, though.

E
 
R

Robert Klemme

Brian said:
keys = ["1", "2", "3", "4", "5"]
vals = ["a", "b", "c", "d", "e", "f", "g"]

I want h = {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"}

right now I do it by:
for i in 0..keys.size-1
h[keys]=vals
end

is there a ruby way, more slick way of doing this? Thanks.


Not as slick as simons version, but never miss a possibility to use
inject.

irb(main):001:0> keys = ["1", "2", "3", "4", "5"]
=> ["1", "2", "3", "4", "5"]

irb(main):002:0> vals = ["a", "b", "c", "d", "e", "f", "g"]
=> ["a", "b", "c", "d", "e", "f", "g"]

irb(main):003:0> keys.zip(vals).inject({}) { | h, (k, v) | h[k] = v;
h } => {"1"=>"a", "2"=>"b", "3"=>"c", "4"=>"d", "5"=>"e"}


Very nice! You clearly seem injected - pardon: infected.
:)

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top