Dan Claudiu Pop
2011-Sep-05  15:40 UTC
How to iterate an array of hashes and return the interested array
Hi folks,
I have the following structure /json like:
{
    -
    items: [
        -
        {
            google_analytics_id: null
            title: "abc"
            type: "station"
            masthead_file: null
            slug: "abcd"
        }
        -
        {
            google_analytics_id: null
            title: "xyz"
            type: "station"
            masthead_file: null
            slug: "xyzt"
        }
etc.
I want to iterate the array of hashes and return for example the
second hash or return the value from the second hash for title key.
Can you provide some guidelines ?
Thank you,
Dan
-- 
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.
7stud --
2011-Sep-05  16:35 UTC
Re: How to iterate an array of hashes and return the interested array
That isn''t valid ruby syntax. Are you trying to parse a file with that syntax? -- 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.
Everaldo Gomes
2011-Sep-05  16:39 UTC
Re: Re: How to iterate an array of hashes and return the interested array
Take a look at this, http://developer.yahoo.com/ruby/ruby-json.html On Mon, Sep 5, 2011 at 1:35 PM, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> That isn''t valid ruby syntax. Are you trying to parse a file with that > syntax? > > -- > 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. > >-- 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.
Ray Parker
2011-Sep-05  16:39 UTC
Re: How to iterate an array of hashes and return the interested array
my_array.select{|item| item[:key] == my_criterion}
..will give you an array of items that meet your needs if the
''item''
is a hash.
On Sep 5, 12:35 pm, 7stud --
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> That isn''t valid ruby syntax.  Are you trying to parse a file with
that
> syntax?
>
> --
> Posted viahttp://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@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Dan Claudiu Pop
2011-Sep-05  16:51 UTC
Re: How to iterate an array of hashes and return the interested array
I already parsed the json call. The idea is that i want to access a certain hash within :item => [] or return the slug value for title => "xyz" On Sep 5, 7:39 pm, Everaldo Gomes <everaldo.go...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Take a look at this,http://developer.yahoo.com/ruby/ruby-json.html > > > > > > > > On Mon, Sep 5, 2011 at 1:35 PM, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > That isn''t valid ruby syntax. Are you trying to parse a file with that > > syntax? > > > -- > > Posted viahttp://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.-- 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.
7stud --
2011-Sep-05  17:11 UTC
Re: How to iterate an array of hashes and return the interested array
Well, it looks to me like you have an outer hash with key/values, and a 
value is an array of hashes, so you would do this:
main_hash["items"][1]["title"] ==>
''xyz''
main_hash["items"][1]["title"] ==>
''xyz''
             ^     ^
             |     |
           array   |
                   |
                  hash
-- 
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.