[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

merge two arrays into a hash

Jan Ask

9/26/2007 6:47:00 AM

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

9 Answers

Christian

9/26/2007 6:58:00 AM

0

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


--

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

Stefano Crocco

9/26/2007 7:01:00 AM

0

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

Farrel Lifson

9/26/2007 7:02:00 AM

0

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?

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}

Daniel Sheppard

9/26/2007 7:05:00 AM

0

> 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


Jan Ask

9/26/2007 7:05:00 AM

0

Thanks to all of you!

I now have a beautiful hash that my mother would be proud of :)
--
Posted via http://www.ruby-....

Daniel Sheppard

9/26/2007 7:08:00 AM

0


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

Daniel Sheppard

9/26/2007 7:49:00 AM

0

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

David A. Black

9/26/2007 8:09:00 AM

0

Phrogz

9/26/2007 2:10:00 PM

0

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