Hello everybody!
I have a problem with an Hash of Arrays.
ruby-1.9.2-p136 :001 > x = Hash.new Array.new
=> {}
ruby-1.9.2-p136 :002 > x["test"] << "test"
=> ["test"]
ruby-1.9.2-p136 :003 > x
=> {}
ruby-1.9.2-p136 :004 > x.keys
=> []
ruby-1.9.2-p136 :005 > x["test"]
=> ["test"]
ruby-1.9.2-p136 :006 >
Why is keys empty?
What I am trying to do is build a hash of arrays from an XML file:
b = Hash.new(Array.new)
h = XmlSimple.xml_in self.raw
h["data"][0]["bank"][0]["item"].each do
|item|
b[item["cat"]] << item["name"]
end
This is the code, but b has like the test in irb no keys, or seems to
be empty, unless you dont know the key i advance, but that is
impossible, since the item["cat"] is dynamic and it is known of 10
categories at the moment, but it is also known that the number of
categories will go up in the future, so I want to do it future-proof
already today :D
Thank you in Advance
Norbert
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 11, 9:09 pm, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hello everybody! > > I have a problem with an Hash of Arrays. > > ruby-1.9.2-p136 :001 > x = Hash.new Array.new > => {} > ruby-1.9.2-p136 :002 > x["test"] << "test" > => ["test"] > ruby-1.9.2-p136 :003 > x > => {} > ruby-1.9.2-p136 :004 > x.keys > => [] > ruby-1.9.2-p136 :005 > x["test"] > => ["test"] > ruby-1.9.2-p136 :006 > > > Why is keys empty? >because you''ve never actually done x["test"]= ... (all you''re doing is modifying the hash''s default value) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/2/11 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > On Feb 11, 9:09 pm, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:>> Why is keys empty? >> > because you''ve never actually done x["test"]= ... > (all you''re doing is modifying the hash''s default value)OK, now I tried the following b = Hash.new(Array.new) h = XmlSimple.xml_in self.raw h["data"][0]["bank"][0]["item"].each do |item| if b.key? item["cat"] b[item["cat"]] = item else b[item["cat"]] << item end end pp b.keys but b.keys is still empty... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> OK, now I tried the following > > b = Hash.new(Array.new) > h = XmlSimple.xml_in self.raw > > h["data"][0]["bank"][0]["item"].each do |item|delete line> if b.key? item["cat"] add line> if !b.key? item["cat"]> b[item["cat"]] = item > else > b[item["cat"]] << item > end > end > > pp b.keyscall me stupid... But with above marked change it works as I want it to behave :D Thanks for the help! Norbert -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Feb 12, 2011 at 9:25 AM, Norbert Melzer <timmelzer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:>> OK, now I tried the following >> b = Hash.new(Array.new) >> h = XmlSimple.xml_in self.raw >> h["data"][0]["bank"][0]["item"].each do |item| > delete line> if b.key? item["cat"] > add line> if !b.key? item["cat"] >> b[item["cat"]] = item >> else >> b[item["cat"]] << item >> end >> end >> >> pp b.keys > > call me stupid... But with above marked change it works as I want it > to behave :Dtry this, b = Hash.new {|h,k| h[k]=[]} h = XmlSimple.xml_in self.raw h["data"][0]["bank"][0]["item"].each do |item| b[item["cat"]] << item end best regards -botp -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 12, 12:47 am, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2011/2/11 Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > On Feb 11, 9:09 pm, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> Why is keys empty? > > > because you''ve never actually done x["test"]= ... > > (all you''re doing is modifying the hash''s default value) > > OK, now I tried the following > > b = Hash.new(Array.new) > h = XmlSimple.xml_in self.raw > > h["data"][0]["bank"][0]["item"].each do |item| > if b.key? item["cat"] > b[item["cat"]] = item > else > b[item["cat"]] << itemYou''re still essentially committing the same error - you''re only setting b[item[''cat''] if there is already an entry for b[item[''cat'']] Did you really mean to use a hash with a single array as its default value? (The default value stuff doesn''t seem to be helping you at all, just muddying the waters Fred> end > end > > pp b.keys > > but b.keys is still empty...-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/2/12 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > On Feb 12, 12:47 am, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> 2011/2/11 Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:>> h["data"][0]["bank"][0]["item"].each do |item| >> if b.key? item["cat"] >> b[item["cat"]] = item >> else >> b[item["cat"]] << item > > You''re still essentially committing the same error - you''re only > setting b[item[''cat''] if there is already an entry for b[item[''cat'']] > Did you really mean to use a hash with a single array as its default > value? (The default value stuff doesn''t seem to be helping you at all, > just muddying the watersAs I mentioned above, my version did it after I introduced "if !b.key? [...]". But I will test the code that botp gave me too, seems to be more DRY :D -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Norbert:
An idiom that I use frequently is:
h = {}
(h[key] ||= []) << item
which essentially says "if hash h[key] is nil, create an empty array for
that key. then push item onto the array. for example:
>> h = {}
=> {}>> (h["cats"] ||= []) << "Jellicle"
=> ["Jellicle"]>> (h["cats"] ||= []) << "Mr. Mistoffelees"
=> ["Jellicle", "Mr. Mistoffelees"]
Is that what you were looking for?
- ff
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.