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



-- 
Have a look: http://www.flickr.com/photos/fussel-foto/