Merge two arrays into a hash

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

irb(main):001:0> key = [ “1”, “2”, “3”]
=> [“1”, “2”, “3”]
irb(main):002:0> value = [ “a”, “b” ]
=> [“a”, “b”]
irb(main):003:0> myhash = {}
=> {}
irb(main):004:0> key.each_with_index {|k,i|myhash[k] = value[i]}
=> [“1”, “2”, “3”]
irb(main):005:0> myhash
=> {“1”=>“a”, “2”=>“b”, “3”=>nil}

On 9/26/07, Jan A. [email protected] wrote:

myhash = {‘1’ => ‘a’ , ‘2’ => ‘b’ , ‘3’ => ‘’}

Can this be done without creating new classes and methods?

Thanks a lot

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

“Every child has many wishes. Some include a wallet, two chicks and a
cigar, but that’s another story.”

Alle mercoledì 26 settembre 2007, Jan A. ha scritto:

myhash = {‘1’ => ‘a’ , ‘2’ => ‘b’ , ‘3’ => ‘’}

Can this be done without creating new classes and methods?

Thanks a lot

require ‘generator’
h = {}
SyncEnumerator.new(key, value).each{|i| h[i[0]] = (i[1]|| “”)}
p h
=> {‘1’=>‘a’, ‘2’=>‘b’, ‘3’=>’’}

I hope this helps

Stefano

On 26/09/2007, Jan A. [email protected] wrote:

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}

Thanks to all of you!

I now have a beautiful hash that my mother would be proud of :slight_smile:

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]

For the general case:

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

Dan

require ‘generator’
h = {}
SyncEnumerator.new(key, value).each{|i| h[i[0]] = (i[1]|| “”)}
p h
=> {‘1’=>‘a’, ‘2’=>‘b’, ‘3’=>’’}

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] = v||’’}

Gives you the same result.

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

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

The result is in my_hash

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

On 9/26/07, Daniel S. [email protected] wrote:

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

For the general case:

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

Dan

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

Hi –

On Wed, 26 Sep 2007, Daniel S. wrote:

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

On Sep 26, 2:09 am, “David A. Black” [email protected] wrote:

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!)

On 9/26/07, Daniel S. [email protected] wrote:

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

Thanks a lot! By the way, what is the different between special case and
general case?

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

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

For the general case:

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

Dan