Forum: Ruby Creating Hash of Arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
074597b4e64c139d5e1d51c52e475de2?d=identicon&s=25 Paul D. Kraus (Guest)
on 2006-05-22 22:30
(Received via mailing list)
How can I create a hash that contains an array for its value? How can I
then
loop through the hash keys and intern the contained arrays.

Pseudo Code
------------------
myhash['customercode'][0]=Firstname
myhash['customercode'][1]=M.I
myhash['customercode'][2]=Lastname

myhash.each do |k,v|
  myhash['k'].each do |elm|
    puts "#{k} -> elm"
  end
end
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-05-22 22:38
(Received via mailing list)
myhash['customercode'] = Array.new
> myhash['customercode'][0]=Firstname
> myhash['customercode'][1]=M.I
> myhash['customercode'][2]=Lastname
>
> myhash.each do |el|
   el.each do |elm|
>     puts "#{k} -> elm"
>   end
> end

Should work. Also, if you want to have every item of the hash default
to an array, something like this to construct the Hash is what you
want:

 h = Hash.new { |hash, key| hash[key] = Array.new }

Douglas
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 Simon Kröger (Guest)
on 2006-05-22 22:38
(Received via mailing list)
Paul D. Kraus schrieb:
> myhash.each do |k,v|
>  myhash['k'].each do |elm|
>    puts "#{k} -> elm"
>  end
> end

ruby code:
------------------
myhash = Hash.new{|h, k| h[k] = [nil] * 3}

myhash['customercode'][0]='Firstname'
myhash['customercode'][1]='M.I'
myhash['customercode'][2]='Lastname'

myhash.each do |k,v|
 myhash[k].each do |elm|
   puts "#{k} -> #{elm}"
 end
end

output:
-------------------
customercode -> Firstname
customercode -> M.I
customercode -> Lastname

does that help?

Simon
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-22 22:42
(Received via mailing list)
On May 22, 2006, at 4:28 PM, Paul D. Kraus wrote:

> myhash.each do |k,v|
>  myhash['k'].each do |elm|
>    puts "#{k} -> elm"
>  end
> end

hsh = Hash.new { |this_hash, key_that_didnt_exist| this_hash
[key_that_didnt_exist] = [] }
074597b4e64c139d5e1d51c52e475de2?d=identicon&s=25 Paul D. Kraus (Guest)
on 2006-05-22 22:45
(Received via mailing list)
>
>
> h = Hash.new { |hash, key| hash[key] = Array.new }


Newbie here but what exactly does this like do.
Can you someone break it down for me thanks.
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-05-22 22:53
(Received via mailing list)
On 5/22/06, Paul D. Kraus <paul.kraus@gmail.com> wrote:
> >
> >
> > h = Hash.new { |hash, key| hash[key] = Array.new }
>
>
> Newbie here but what exactly does this like do.
> Can you someone break it down for me thanks.

This creates a new hash with a "default block" which will be called
whenever someone tries to get a value for a non-existent key. When the
block is called a new Array is created for the given key value and
stored in the hash. Therefore you can do things like the following and
they will just magically work:

h[:people] << "Paul"
h[:people] << "Douglas"
h[:people] << "Simon"
h[:animals] << "Dog"
h[:animals] << "Cat"
h[:animals] << "Armadillo"

Ryan
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-05-22 22:56
(Received via mailing list)
2006/5/22, Paul D. Kraus <paul.kraus@gmail.com>:
> >
> >
> > h = Hash.new { |hash, key| hash[key] = Array.new }
>
>
> Newbie here but what exactly does this like do.
> Can you someone break it down for me thanks.
>

From the docs: http://www.ruby-doc.org/core/classes/Hash.html#M000886

If you try and get the value using a hash key which does not exist,
then the value of the block when called with the hash and the new key
will be returned instead.

Douglas
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-23 18:06
(Received via mailing list)
2006/5/22, Paul D. Kraus <paul.kraus@gmail.com>:
> How can I create a hash that contains an array for its value? How can I then
> loop through the hash keys and intern the contained arrays.
>
> Pseudo Code
> ------------------
> myhash['customercode'][0]=Firstname
> myhash['customercode'][1]=M.I
> myhash['customercode'][2]=Lastname

Another solution picking up Daniel's suggestion:

>> Item = Struct.new :first_name, :mi, :last_name
=> Item
>> myhash = Hash.new {|h,k| h[k]=Item.new}
=> {}
>> myhash['code'].first_name = "first"
=> "first"
>> myhash['code'].mi = "mi"
=> "mi"
>> myhash['code'].last_name = "last"
=> "last"
>> myhash['code']
=> #<struct Item first_name="first", mi="mi", last_name="last">

> myhash.each do |k,v|
>   myhash['k'].each do |elm|
>     puts "#{k} -> elm"
>   end
> end

You're making things more complicated than necessary.  This will work
(with you array approach and with my suggestion):

>> myhash.each do |k, v|
?> v.each {|elm| puts "#{k} -> #{elm}" }
>> end
code -> first
code -> mi
code -> last
=> {"code"=>#<struct Item first_name="first", mi="mi",
last_name="last">}

alternative

>> myhash.each {|k,it| it.each {|val| print k, " -> ", val, "\n"} }
code -> first
code -> mi
code -> last
=> {"code"=>#<struct Item first_name="first", mi="mi",
last_name="last">}

Btw, also you use 'k' as a key in your iteration - you must remove the
single quotes to make it work the way you want.

Kind regards

robert
This topic is locked and can not be replied to.