merge two arrays into a hash

J

Jan Ask

Hi all,

I have looked at some of the answers in the forum, but they do not seem
to fit. I want to merge two array into a hash like so:

key = [ "1", "2", "3"]
value = [ "a", "b" ]

into:

myhash = {'1' => 'a' , '2' => 'b' , '3' => ''}

Can this be done without creating new classes and methods?

Thanks a lot
 
S

Stefano Crocco

Alle mercoled=C3=AC 26 settembre 2007, Jan Ask ha scritto:
Hi all,

I have looked at some of the answers in the forum, but they do not seem
to fit. I want to merge two array into a hash like so:

key =3D [ "1", "2", "3"]
value =3D [ "a", "b" ]

into:

myhash =3D {'1' =3D> 'a' , '2' =3D> 'b' , '3' =3D> ''}

Can this be done without creating new classes and methods?

Thanks a lot

require 'generator'
h =3D {}
SyncEnumerator.new(key, value).each{|i| h[i[0]] =3D (i[1]|| "")}
p h
=3D> {'1'=3D>'a', '2'=3D>'b', '3'=3D>''}

I hope this helps

Stefano
 
F

Farrel Lifson

Hi all,

I have looked at some of the answers in the forum, but they do not seem
to fit. I want to merge two array into a hash like so:

key = [ "1", "2", "3"]
value = [ "a", "b" ]

into:

myhash = {'1' => 'a' , '2' => 'b' , '3' => ''}

Can this be done without creating new classes and methods?

irb(main):001:0> key = ["1","2","3"]
=> ["1", "2", "3"]
irb(main):002:0> value = ["a","b","c"]
=> ["a", "b"]
irb(main):009:0> Hash[*key.zip(value).flatten]
=> {"1"=>"a", "2"=>"b", "3"=>nil}
 
D

Daniel Sheppard

key =3D [ "1", "2", "3"]
value =3D [ "a", "b" ]
=20
into:
=20
myhash =3D {'1' =3D> 'a' , '2' =3D> 'b' , '3' =3D> ''}

For the special case where key and value do not contain arrays:

Hash[*key.zip(value).flatten]

For the general case:

myhash =3D {}
key.zip(value) {|a,b| myhash[a] =3D b }

Dan
 
D

Daniel Sheppard

=20
require 'generator'
h =3D {}
SyncEnumerator.new(key, value).each{|i| h[i[0]] =3D (i[1]|| "")}
p h
=3D> {'1'=3D>'a', '2'=3D>'b', '3'=3D>''}

When an array is being passed to a block, you can just provide args for
the whole array:

SyncEnumerator.new(key, value).each{|k,v| h[k] =3D v||''}

Gives you the same result.
 
D

Daniel Sheppard

myhash =3D {}
key.zip(value) {|a,b| myhash[a] =3D b }

It does not work!
irb(main):017:0> key.zip(value){|a,b| myhash[a] =3D b}
=3D> nil

The result is in my_hash

irb(main):014:0> myhash =3D {}
=3D> {}
irb(main):015:0> key.zip(value) {|a,b| myhash[a] =3D b }
=3D> nil
irb(main):016:0> myhash
=3D> {"1"=3D>"a", "2"=3D>"b", "3"=3D>nil}
 
D

David A. Black

Hi --

key = [ "1", "2", "3"]
value = [ "a", "b" ]

into:

myhash = {'1' => 'a' , '2' => 'b' , '3' => ''}

For the special case where key and value do not contain arrays:

Hash[*key.zip(value).flatten]

And in 1.9 you can do: flatten(1)


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!
 
P

Phrogz

And in 1.9 you can do: flatten(1)

Hot dog! Come on lucky number 1.9, you're just what I wanted!

(Seriously, I've used David's flatten_n library many times in the
past, but kept trying to wean myself from it since it wasn't in the
core or stdlib. How nice it will be to have that functionality
generally available!)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top