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

···

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

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 Ask <janaskhoej@gmail.com> wrote:

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
--
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 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 = [ "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

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

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}

···

On 26/09/2007, Jan Ask <janaskhoej@gmail.com> wrote:

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?

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

Thanks to all of you!

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

···

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

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.

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

···

On 9/26/07, Daniel Sheppard <daniels@pronto.com.au> 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]

For the general case:

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

Dan

It does not work!

--
Best Regards
Cooper Geng

Hi --

···

On Wed, 26 Sep 2007, Daniel Sheppard 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

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

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

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 Sep 26, 2:09 am, "David A. Black" <dbl...@rubypal.com> wrote:

And in 1.9 you can do: flatten(1)

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

···

On 9/26/07, Daniel Sheppard <daniels@pronto.com.au> wrote:

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

--
Best Regards
Cooper Geng