jandot
September 26, 2007, 8:47am
1
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
jandot
September 26, 2007, 8:58am
2
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.”
jandot
September 26, 2007, 9:02am
3
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
jandot
September 26, 2007, 9:03am
4
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}
jandot
September 26, 2007, 9:05am
5
Thanks to all of you!
I now have a beautiful hash that my mother would be proud of
jandot
September 26, 2007, 9:06am
6
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
jandot
September 26, 2007, 9:08am
7
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.
jandot
September 26, 2007, 9:49am
8
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}
jandot
September 26, 2007, 9:24am
9
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
jandot
September 26, 2007, 10:09am
10
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
jandot
September 26, 2007, 4:11pm
11
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!)
jandot
September 26, 2007, 9:57am
12
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