Iterating a complex data structure

Hi, I'm new to the list and new to ruby. I have an data like so:
stuff = [ { name: foo, tags: { one: blah, two: blah } }, etc ]

I'm iterating like so:
stuff.each do |h|
        name = h[:name]
        tags = h[:tags]
....
end

Is there a way to get the name value and the tags hash straight out of
the .each that is less awkward than what I have?

Thanks for any suggestions.

···

--
Posted via http://www.ruby-forum.com/.

Excerpts from Mark K.'s message of 2013-12-21 16:07:29 +0100:

Hi, I'm new to the list and new to ruby. I have an data like so:
stuff = [ { name: foo, tags: { one: blah, two: blah } }, etc ]

I'm iterating like so:
stuff.each do |h|
        name = h[:name]
        tags = h[:tags]
....
end

Is there a way to get the name value and the tags hash straight out of
the .each that is less awkward than what I have?

Thanks for any suggestions.

I'm not sure what exactly you need. The following gives you a nested
array where each inner array has the name as first element and the tags
as second element. However, you still have to navigate this array.

stuff.map{|h| [h[:name], h[:tags]]}

In your example, this would give:

[[:foo, {one: blah, two: blah}, ...]

If this doesn't help, I think you'll need to tell us more about what you
want to achieve.

Stefano

Quoting Mark K. (lists@ruby-forum.com):

Hi, I'm new to the list and new to ruby. I have an data like so:
stuff = [ { name: foo, tags: { one: blah, two: blah } }, etc ]

I'm iterating like so:
stuff.each do |h|
        name = h[:name]
        tags = h[:tags]
....
end

Is there a way to get the name value and the tags hash straight out of
the .each that is less awkward than what I have?

If, instead of an array of hashes, you go for a hash of hashes, where
the key of the main hash is your name, like this:

stuff={foo:{one:blah,two:blah},bar:{one:bleh,two:bleh}...}

then you can iterate it like this

stuff.each do |key,value|

where key is your name and value is your tags hash. Of course, you
won't be able to have two elements with the same name (which you could
do in your example).

Carlo

···

Subject: iterating a complex data structure
  Date: sab 21 dic 13 04:07:29 +0100

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Stefano Crocco wrote in post #1131358:

I'm not sure what exactly you need. The following gives you a nested
array where each inner array has the name as first element and the tags
as second element. However, you still have to navigate this array.

stuff.map{|h| [h[:name], h[:tags]]}

In your example, this would give:

[[:foo, {one: blah, two: blah}, ...]

If this doesn't help, I think you'll need to tell us more about what you
want to achieve.

Stefano

I was hoping to be handed the value of :name and :tags right out of an
iterator in one fell swoop. Maybe its just a two step process.

···

--
Posted via http://www.ruby-forum.com/\.

Carlo E. Prelz wrote in post #1131361:

If, instead of an array of hashes, you go for a hash of hashes, where
the key of the main hash is your name, like this:

stuff={foo:{one:blah,two:blah},bar:{one:bleh,two:bleh}...}

then you can iterate it like this

stuff.each do |key,value|

where key is your name and value is your tags hash. Of course, you
won't be able to have two elements with the same name (which you could
do in your example).

Carlo

Ah, that's a better idea. Thanks.

···

--
Posted via http://www.ruby-forum.com/\.

stuff.each do |h|
  (first_key, first_value),(second_key, second_value) = *h
end

Awkward ** 2 ?!? :wink:

Best regards,
Abinoam Jr.

···

On Sat, Dec 21, 2013 at 7:00 PM, Mark K. <lists@ruby-forum.com> wrote:

Carlo E. Prelz wrote in post #1131361:

If, instead of an array of hashes, you go for a hash of hashes, where
the key of the main hash is your name, like this:

stuff={foo:{one:blah,two:blah},bar:{one:bleh,two:bleh}...}

then you can iterate it like this

stuff.each do |key,value|

where key is your name and value is your tags hash. Of course, you
won't be able to have two elements with the same name (which you could
do in your example).

Carlo

Ah, that's a better idea. Thanks.

--
Posted via http://www.ruby-forum.com/\.