# Array iterator that have more arrays that also need iteratio

**URL:** <https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808>\
**Category:** ruby-talk\
**Created:** [6 December 2007 19:25 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808 "2007-12-06T19:25:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 19:25 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/1 "2007-12-06T19:25:29Z")

</div>

Hello ...

In my new introduction to RoR and Ruby, I'm stopped again ...

I know it must be easy, but I can't figure it out, maybe I'm stucked ...

I have one array, and each element of this array, has another array.

I have to loop over two arrays, and extracting all the values, without  
knowing their attributes ...

I tried with array.each, array.each\_index, ...

here is may array dumped:

- - !ruby/object:InvoicesCalcul  
&nbsp;&nbsp;&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_base: "82567.36"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_value: "95778.21"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_tax: "13210.85"  
- - !ruby/object:InvoicesCalcul  
&nbsp;&nbsp;&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_tax: "17.91"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_base: "255.81"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_value: "273.72"  
- - !ruby/object:InvoicesCalcul  
&nbsp;&nbsp;&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_base: "707.59"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_tax: "0.0"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_value: "707.59"

I need:

82567.36 95778.21 13210.85  
17.91 255.81 273.72  
707.59 0.0 707.59

and here is the last I tried ...  
the dumped array is: @arr  
the @arr\_tax is another array, but this works

what I'm getting is the object or something strange, no errors.

\<%@arr.each\_index{|x| %\>  
\<tr\>  
\<td align=Right width="50px"\> (x\<%= x%\>) \<%= @arr\_tax[x]%\>\</td\>

\<%@arr[x].each{|y| %\>  
\<td align=Right\>(y\<%= y%\>) \<%= y[x]%\>\</td\>  
\<% }%\>  
\</tr\>  
\<% }%\>

thanks!

raimon

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [6 December 2007 19:44 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/2 "2007-12-06T19:44:32Z")

</div>

> I have one array, and each element of this array, has another array.

\<snip\>

> here is may array dumped:
> 
> - - !ruby/object:InvoicesCalcul  
> &nbsp;&nbsp;&nbsp;attributes:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_base: "82567.36"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_value: "95778.21"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1600\_tax: "13210.85"  
> - - !ruby/object:InvoicesCalcul  
> &nbsp;&nbsp;&nbsp;attributes:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_tax: "17.91"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_base: "255.81"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_value: "273.72"  
> - - !ruby/object:InvoicesCalcul  
> &nbsp;&nbsp;&nbsp;attributes:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_base: "707.59"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_tax: "0.0"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0\_value: "707.59"
> 
> I need:
> 
> 82567.36 95778.21 13210.85  
> 17.91 255.81 273.72  
> 707.59 0.0 707.59

Looks like you have a collection of InvoicesCalcul, each of them with an accessor "attributes", which is a hash:

&nbsp;&nbsp;&nbsp;values = invoice\_calculs.map do |ic|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
&nbsp;&nbsp;&nbsp;end.map do |a|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.values  
&nbsp;&nbsp;&nbsp;end.flatten

Those could be AR objetcs, in Rails you can write that this way:

&nbsp;&nbsp;&nbsp;values = invoice\_calculs.map(&:attributes).map(&:values).flatten

-- fxn

> **···**
>
> On Dec 6, 2007, at 8:25 PM, Raimon Fs wrote:

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 20:12 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/3 "2007-12-06T20:12:21Z")

</div>

Xavier Noria wrote:

> > I have one array, and each element of this array, has another array.
> 
> \<snip\>
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_tax: "17.91"  
> > 82567.36 95778.21 13210.85  
> > 17.91 255.81 273.72  
> > 707.59 0.0 707.59
> 
> Looks like you have a collection of InvoicesCalcul, each of them with  
> an accessor "attributes", which is a hash:
> 
> &nbsp;&nbsp;&nbsp;values = invoice\_calculs.map do |ic|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
> &nbsp;&nbsp;&nbsp;end.map do |a|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.values  
> &nbsp;&nbsp;&nbsp;end.flatten
> 
> Those could be AR objetcs, in Rails you can write that this way:
> 
> &nbsp;&nbsp;&nbsp;values = invoice\_calculs.map(&:attributes).map(&:values).flatten

thanks ...

maybe they are AR, because I get those values from a loop, where I issue  
some sql:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@temp\_result = InvoicesCalcul.find\_by\_sql('SELECT sum(base) AS  
"'+ @name\_base + '" ,sum(import) AS "' + @name\_tax + '" ,sum(total\_net)  
AS "' + @name\_total + '" FROM invoices\_calculs c, invoices i WHERE  
c.tipus='+KIND\_TAX\_IVA.to\_s+' AND c.invoice\_id=i.id AND c.xcent='+  
@value +' AND i.any='+@c\_year)

and I add those results into an array.

what I see, with your suggestion, I have the same issue as before, I  
want to retrieve the values without knowing the attribute, only by the  
index:

now I have to a['1600\_base'] to get the value, I just want the value for  
each index, not the attribute.  
i'm going to deep into the rail's book ...

thanks,

raimon

> **···**
>
> > On Dec 6, 2007, at 8:25 PM, Raimon Fs wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 20:33 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/4 "2007-12-06T20:33:29Z")

</div>

Raimon Fs wrote:

> Xavier Noria wrote:
> 
> > > I have one array, and each element of this array, has another array.
> > 
> > \<snip\>
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;700\_tax: "17.91"  
> > > 82567.36 95778.21 13210.85  
> > > 17.91 255.81 273.72  
> > > 707.59 0.0 707.59
> > 
> > Looks like you have a collection of InvoicesCalcul, each of them with  
> > an accessor "attributes", which is a hash:
> > 
> > &nbsp;&nbsp;&nbsp;values = invoice\_calculs.map do |ic|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
> > &nbsp;&nbsp;&nbsp;end.map do |a|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.values  
> > &nbsp;&nbsp;&nbsp;end.flatten

after reading some docs, it would be possible this:

a.each\_value {|value| puts value }

but I'm getting this:  
1600\_base82567.36000000011600\_value95778.211600\_tax13210.85

and i want only the values, and it seems that puts in just one time all  
the key+values, not in a loop.

debug(a) gives me: ---  
1600\_base: "82567.3600000001"  
1600\_value: "95778.21"  
1600\_tax: "13210.85"

and in pseudo code, I want this:

\<td\> a.value \</td\> for each value

thanks again,

raimon

> **···**
>
> > > On Dec 6, 2007, at 8:25 PM, Raimon Fs wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [6 December 2007 20:40 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/5 "2007-12-06T20:40:26Z")

</div>

> maybe they are AR, because I get those values from a loop, where I issue  
> some sql:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@temp\_result = InvoicesCalcul.find\_by\_sql('SELECT sum(base) AS  
> "'+ @name\_base + '" ,sum(import) AS "' + @name\_tax + '" ,sum(total\_net)  
> AS "' + @name\_total + '" FROM invoices\_calculs c, invoices i WHERE  
> c.tipus='+KIND\_TAX\_IVA.to\_s+' AND c.invoice\_id=i.id AND c.xcent='+  
> @value +' AND i.any='+@c\_year)
> 
> and I add those results into an array.

@temp\_result is already an array of ARs.

> what I see, with your suggestion, I have the same issue as before, I  
> want to retrieve the values without knowing the attribute, only by the  
> index:

No, no. Please reread the code, it extracts the values without touching the keys. I think that's what you want.

-- fxn

> **···**
>
> On Dec 6, 2007, at 9:12 PM, Raimon Fs wrote:

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 21:02 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/6 "2007-12-06T21:02:22Z")

</div>

Xavier Noria wrote:

> > and I add those results into an array.
> 
> @temp\_result is already an array of ARs.

yes, I mean I add this array into another array:

@arr.push(@temp\_result)

> > what I see, with your suggestion, I have the same issue as before, I  
> > want to retrieve the values without knowing the attribute, only by the  
> > index:
> 
> No, no. Please reread the code, it extracts the values without  
> touching the keys. I think that's what you want.

yes, but the values where written at once:

82567.3695778.2113210.85

and I want separated, for adding them into a cell of a table, with  
\<td\>value\</td\>

thanks,

rai

> **···**
>
> > On Dec 6, 2007, at 9:12 PM, Raimon Fs wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [6 December 2007 21:33 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/7 "2007-12-06T21:33:53Z")

</div>

Then you are done removing the call to flatten:

&nbsp;&nbsp;&nbsp;invoice\_calculs.map(&:attributes).map(&:values)

-- fxn

> **···**
>
> On Dec 6, 2007, at 10:02 PM, Raimon Fs wrote:
> 
> > yes, but the values where written at once:
> > 
> > 82567.3695778.2113210.85
> > 
> > and I want separated, for adding them into a cell of a table, with  
> > \<td\>value\</td\>

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 22:33 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/8 "2007-12-06T22:33:36Z")

</div>

Xavier Noria wrote:

> > yes, but the values where written at once:
> > 
> > 82567.3695778.2113210.85
> > 
> > and I want separated, for adding them into a cell of a table, with  
> > \<td\>value\</td\>
> 
> Then you are done removing the call to flatten:
> 
> &nbsp;&nbsp;&nbsp;invoice\_calculs.map(&:attributes).map(&:values)
> 
> -- fxn

I'm sorry but I don't understand ...

I'm using your first suggestion:

> &nbsp;&nbsp;&nbsp;values = invoice\_calculs.map do |ic|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
> &nbsp;&nbsp;&nbsp;end.map do |a|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.values  
> &nbsp;&nbsp;&nbsp;end.flatten

with or without the .flatten all the values are written at the same time  
...

your new suggestion : invoice\_calculs.map(&:attributes).map(&:values)

also writtes the three values at one time, I need each value separated:

element one:  
82567.3695778.2113210.85

and I need:  
82567.36  
95778.21  
13210.85

I hope it's more clear what I'm trying to do now ...

🙂

regards, and thanks!

rai

> **···**
>
> > On Dec 6, 2007, at 10:02 PM, Raimon Fs wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [6 December 2007 22:51 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/9 "2007-12-06T22:51:55Z")

</div>

The version with flatten gives you all the values in a row in a single array. From the YAML dump that would be

&nbsp;&nbsp;&nbsp;["82567.36", 95778.21", "13210.85", "17.91", "255.81", "273.72", ...]

The version without flatten gives an array of arrays. Each array element contains the values corresponding to a single InvoicesCalcul:

&nbsp;&nbsp;&nbsp;[["82567.36", 95778.21", "13210.85"], ["17.91", "255.81", "273.72"], ...]

Please use that array notation to indicate how would the desired result look like if it is none of those.

-- fxn

> **···**
>
> On Dec 6, 2007, at 11:33 PM, Raimon Fs wrote:
> 
> > I'm using your first suggestion:
> > 
> > > &nbsp;&nbsp;values = invoice\_calculs.map do |ic|  
> > > &nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
> > > &nbsp;&nbsp;end.map do |a|  
> > > &nbsp;&nbsp;&nbsp;&nbsp;a.values  
> > > &nbsp;&nbsp;end.flatten
> > 
> > with or without the .flatten all the values are written at the same time  
> > ...
> > 
> > your new suggestion : invoice\_calculs.map(&:attributes).map(&:values)
> > 
> > also writtes the three values at one time, I need each value separated:
> > 
> > element one:  
> > 82567.3695778.2113210.85
> > 
> > and I need:  
> > 82567.36  
> > 95778.21  
> > 13210.85
> > 
> > I hope it's more clear what I'm trying to do now ...

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [6 December 2007 23:13 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/10 "2007-12-06T23:13:19Z")

</div>

Xavier Noria wrote:

> > ...  
> > 82567.36  
> > 95778.21  
> > 13210.85
> > 
> > I hope it's more clear what I'm trying to do now ...
> 
> The version with flatten gives you all the values in a row in a single  
> array. From the YAML dump that would be
> 
> &nbsp;&nbsp;&nbsp;["82567.36", 95778.21", "13210.85", "17.91", "255.81", "273.72", ...]
> 
> The version without flatten gives an array of arrays. Each array  
> element contains the values corresponding to a single InvoicesCalcul:
> 
> &nbsp;&nbsp;&nbsp;[["82567.36", 95778.21", "13210.85"], ["17.91", "255.81",  
> "273.72"], ...]
> 
> Please use that array notation to indicate how would the desired  
> result look like if it is none of those.
> 
> -- fxn

I must be missing something, because if I make a debug(a):

values = @arr[0].map do |ic|  
&nbsp;&nbsp;&nbsp;&nbsp;ic.attributes  
&nbsp;&nbsp;end.map do |a|  
&nbsp;&nbsp;&nbsp;# a.values  
debug(a)

end.flatten

1600\_base: "82567.3600000001"  
1600\_value: "95778.21"  
1600\_tax: "13210.85"

I'm still getting attributes and values, not a simple array ...

I'm going to re-check all the code and see where the problem is ...

thanks!

r.

> **···**
>
> > On Dec 6, 2007, at 11:33 PM, Raimon Fs wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [7 December 2007 00:00 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/11 "2007-12-07T00:00:12Z")

</div>

But why do you put a trace at that point? Of course a is a hash there, but you are not aggregating the hash, you're collecting the \*collection of values of those hashes\* via a.values. You need to work with tha variable "values" which is assigned the result of the map.chain. Please inspect the array stored in the variable "values" after that block of code has run.

-- fxn

> **···**
>
> On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:
> 
> > values = @arr[0].map do |ic|  
> > &nbsp;&nbsp;&nbsp;ic.attributes  
> > end.map do |a|  
> > &nbsp;&nbsp;# a.values  
> > debug(a)
> > 
> > end.flatten
> > 
> > 1600\_base: "82567.3600000001"  
> > 1600\_value: "95778.21"  
> > 1600\_tax: "13210.85"
> > 
> > I'm still getting attributes and values, not a simple array ...

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [7 December 2007 08:06 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/12 "2007-12-07T08:06:04Z")

</div>

Xavier Noria wrote:

> > 1600\_tax: "13210.85"
> > 
> > I'm still getting attributes and values, not a simple array ...
> 
> But why do you put a trace at that point? Of course a is a hash there,  
> but you are not aggregating the hash, you're collecting the  
> \*collection of values of those hashes\* via a.values. You need to work  
> with tha variable "values" which is assigned the result of the  
> map.chain. Please inspect the array stored in the variable "values"  
> after that block of code has run.

Now I see where was my error ... I was using the variable a instead of  
variable values.

Using the values gives me an array, but now I see that the order is not  
the expected.

It's the same as it was in the hash, so the problem is at the begining,  
when I issue the sql:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@temp\_result = InvoicesCalcul.find\_by\_sql('SELECT sum(base) AS  
"'+ @name\_base + '" ,sum(import) AS "' + @name\_tax + '" ,sum(total\_net)  
AS "' + @name\_total + '" FROM invoices\_calculs c, invoices i WHERE  
c.tipus='+KIND\_TAX\_IVA.to\_s+' AND c.invoice\_id=i.id AND c.xcent='+  
@value +' AND i.any='+@c\_year)

I suppose, that @temp\_result should be:

> **···**
>
> > On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:
> 
> ---  
> - !ruby/object:InvoicesCalcul  
> &nbsp;&nbsp;attributes:  
> &nbsp;&nbsp;&nbsp;&nbsp;base: "707.59"  
> &nbsp;&nbsp;&nbsp;&nbsp;tax: "0.0"  
> &nbsp;&nbsp;&nbsp;&nbsp;total: "707.59"
> 
> but it's:  
> ---  
> - !ruby/object:InvoicesCalcul  
> &nbsp;&nbsp;attributes:  
> &nbsp;&nbsp;&nbsp;&nbsp;tax: "0.0"  
> &nbsp;&nbsp;&nbsp;&nbsp;total: "707.59"  
> &nbsp;&nbsp;&nbsp;&nbsp;base: "707.59"
> 
> so, I have to change my code, but at least, I learned how to extract the  
> values from a hash and put in an array.
> 
> Thanks, now is working, but I did some changes in other parts.
> 
> regards,
> 
> raimon  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [7 December 2007 09:15 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/13 "2007-12-07T09:15:25Z")

</div>

Hashes are unordered in ruby 1.8. The best way to guarantee you always  
get the order you expect is to sort the keys. Using Xavier's code from  
above with "values" result sorted on hash key...

values = @arr[0].map do |ic|  
&nbsp;&nbsp;ic.attributes  
end.map do |a|  
&nbsp;&nbsp;a.keys.sort.map do |k| # this part  
&nbsp;&nbsp;&nbsp;&nbsp;a[k]  
&nbsp;&nbsp;end  
end.flatten

Regards,  
Jordan

> **···**
>
> On Dec 7, 2:06 am, Raimon Fs \<co...@montx.com\> wrote:
> 
> > Xavier Noria wrote:  
> > \> On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:
> > 
> > \>\> 1600\_tax: "13210.85"
> > 
> > \>\> I'm still getting attributes and values, not a simple array ...
> > 
> > \> But why do you put a trace at that point? Of course a is a hash there,  
> > \> but you are not aggregating the hash, you're collecting the  
> > \> \*collection of values of those hashes\* via a.values. You need to work  
> > \> with tha variable "values" which is assigned the result of the  
> > \> map.chain. Please inspect the array stored in the variable "values"  
> > \> after that block of code has run.
> > 
> > Now I see where was my error ... I was using the variable a instead of  
> > variable values.
> > 
> > Using the values gives me an array, but now I see that the order is not  
> > the expected.
> > 
> > It's the same as it was in the hash, so the problem is at the begining,  
> > when I issue the sql:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@temp\_result = InvoicesCalcul.find\_by\_sql('SELECT sum(base) AS  
> > "'+ @name\_base + '" ,sum(import) AS "' + @name\_tax + '" ,sum(total\_net)  
> > AS "' + @name\_total + '" FROM invoices\_calculs c, invoices i WHERE  
> > c.tipus='+KIND\_TAX\_IVA.to\_s+' AND c.invoice\_id=i.id AND c.xcent='+  
> > @value +' AND i.any='+@c\_year)
> > 
> > I suppose, that @temp\_result should be:  
> > ---  
> > - !ruby/object:InvoicesCalcul  
> > &nbsp;&nbsp;attributes:  
> > &nbsp;&nbsp;&nbsp;&nbsp;base: "707.59"  
> > &nbsp;&nbsp;&nbsp;&nbsp;tax: "0.0"  
> > &nbsp;&nbsp;&nbsp;&nbsp;total: "707.59"
> > 
> > but it's:  
> > ---  
> > - !ruby/object:InvoicesCalcul  
> > &nbsp;&nbsp;attributes:  
> > &nbsp;&nbsp;&nbsp;&nbsp;tax: "0.0"  
> > &nbsp;&nbsp;&nbsp;&nbsp;total: "707.59"  
> > &nbsp;&nbsp;&nbsp;&nbsp;base: "707.59"
> > 
> > so, I have to change my code, but at least, I learned how to extract the  
> > values from a hash and put in an array.
> > 
> > Thanks, now is working, but I did some changes in other parts.
> > 
> > regards,
> > 
> > raimon  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [7 December 2007 09:22 UTC](https://rubytalk.org/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/42808/14 "2007-12-07T09:22:40Z")

</div>

Jordan Callicoat wrote:

> **···**
>
> > On Dec 7, 2:06 am, Raimon Fs \<co...@montx.com\> wrote:
> > 
> > > \> with tha variable "values" which is assigned the result of the  
> > > when I issue the sql:  
> > > &nbsp;&nbsp;attributes:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;base: "707.59"  
> > > Posted viahttp://www.ruby-forum.com/.
> > 
> > Hashes are unordered in ruby 1.8. The best way to guarantee you always  
> > get the order you expect is to sort the keys. Using Xavier's code from  
> > above with "values" result sorted on hash key...
> > 
> > values = @arr[0].map do |ic|  
> > &nbsp;&nbsp;ic.attributes  
> > end.map do |a|  
> > &nbsp;&nbsp;a.keys.sort.map do |k| # this part  
> > &nbsp;&nbsp;&nbsp;&nbsp;a[k]  
> > &nbsp;&nbsp;end  
> > end.flatten
> > 
> > Regards,  
> > Jordan
> 
> thanks for the explanation, I see I have to learn more Ruby ...
> 
> 🙂
> 
> regards,
> 
> raimon  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
