I have an array of hash : tags_on => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}] and I would like to get the value ["d", "e"] for a specified key :comma is there any DRY way to do it or should I loop into the array ? thanks for your feedback -- 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.
Michael Pavling
2011-Feb-24 19:06 UTC
Re: Array and Hash ... how to find the value ? DRYest way
On 24 February 2011 18:29, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I have an array of hash : > tags_on > => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}]What if your array of hashes looks like this: [{:point=>["a", "b", "c"], :comma=>["f", "g"]}, {:comma=>["d", "e"]}] ..how would you know which :comma key to retrieve? -- 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 24 February 2011 18:29, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I have an array of hash : > tags_on > => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}] > > and I would like to get the value ["d", "e"] for a specified > key :comma > is there any DRY way to do it or should I loop into the array ?Assuming the keys are unique why don''t you build it as a straight hash in the first place? If they are not unique then you are in trouble anyway. If you are stuck with the array then I think I would use collect to convert it into a straight hash first. Colin -- 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.
they wil be unique , before adding a new hash, I''ll have to check if the key already exist but I agree with Colin, better build a straight hash first On 24 fév, 20:06, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24 February 2011 18:29, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > I have an array of hash : > > tags_on > > => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}] > > What if your array of hashes looks like this: > > [{:point=>["a", "b", "c"], :comma=>["f", "g"]}, {:comma=>["d", "e"]}] > > ..how would you know which :comma key to retrieve?-- 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.
you''re right, rather than pushing hash into tags_on , I''ll have to build it as a straight hash On 24 fév, 22:09, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 24 February 2011 18:29, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > I have an array of hash : > > tags_on > > => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}] > > > and I would like to get the value ["d", "e"] for a specified > > key :comma > > is there any DRY way to do it or should I loop into the array ? > > Assuming the keys are unique why don''t you build it as a straight hash > in the first place? If they are not unique then you are in trouble > anyway. If you are stuck with the array then I think I would use > collect to convert it into a straight hash first. > > Colin-- 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.
Michael Pavling
2011-Feb-24 21:44 UTC
Re: Re: Array and Hash ... how to find the value ? DRYest way
On 24 February 2011 21:43, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> they wil be unique , before adding a new hash, I''ll have to check if > the key already exist > but I agree with Colin, better build a straight hash firstThat''s kindof what I was getting at - if the keys are unique, you don''t have an array of hashes, you have a hash! :-) -- 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.
Matt Jones
2011-Feb-25 17:17 UTC
Re: Array and Hash ... how to find the value ? DRYest way
On Feb 24, 1:29 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> I have an array of hash : > tags_on > => [{:point=>["a", "b", "c"]}, {:comma=>["d", "e"]}] > > and I would like to get the value ["d", "e"] for a specified > key :comma > is there any DRY way to do it or should I loop into the array ?For reference, this will do what you''re looking for (array starts in tags_on, key in desired_key): hash = tags_on.detect { |h| h.has_key?(desired_key) } result = hash[desired_key] if hash I''ve had to build little arrays of single-key hashes like this to work around 1.8.6 not having ordered hash semantics. --Matt Jones -- 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.
Michael Pavling
2011-Feb-25 18:52 UTC
Re: Re: Array and Hash ... how to find the value ? DRYest way
On 25 February 2011 17:17, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve had to build little arrays of single-key hashes like this to work > around 1.8.6 not having ordered hash semantics....or you have a normal hash with one of the values as an array of the keys in the order you want (I normally call it ":order"). Then when you want to iterate you hash in order: my_hash = {:name => "fred", :order => [:age, :foo, :name], :age => "21", :foo => "bar"} my_hash[:order].each do |key| puts my_hash[key] end Keeps it all "standard" and in one object... YMMV -- 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.