# Array iterator that have more arrays that also need iteratio

**URL:** <https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344>\
**Category:** Ruby\
**Created:** [December 6, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344 "2007-12-06T20:25:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/1 "2007-12-06T20:25:30Z")

</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  
attributes:  
1600\_base: “82567.36”  
1600\_value: “95778.21”  
1600\_tax: “13210.85”

- 
  - !ruby/object:InvoicesCalcul  
attributes:  
700\_tax: “17.91”  
700\_base: “255.81”  
700\_value: “273.72”

- 
  - !ruby/object:InvoicesCalcul  
attributes:  
0\_base: “707.59”  
0\_tax: “0.0”  
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.

\<removed\_email\_address@domain.invalid\_index{|x| %\>

 (x\<%= x%\>) \<%= @arr\_tax[x]%\>

\<%@arr[x].each{|y| %\>

(y\<%= y%\>) \<%= y[x]%\> \<% }%\> \<% }%\>

thanks!

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/2 "2007-12-06T20:48:21Z")

</div>

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

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

\> 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:

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

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

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

– fxn

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/3 "2007-12-06T21:12:25Z")

</div>

Xavier N. wrote:

> On Dec 6, 2007, at 8:25 PM, Raimon Fs wrote:
> 
> > I have one array, and each element of this array, has another array.
> 
> \> 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:
> 
> values = invoice\_calculs.map do |ic|  
> ic.attributes  
> end.map do |a|  
> a.values  
> end.flatten
> 
> Those could be AR objetcs, in Rails you can write that this way:
> 
> 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:  
@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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/4 "2007-12-06T21:33:33Z")

</div>

Raimon Fs wrote:

> Xavier N. wrote:
> 
> > On Dec 6, 2007, at 8:25 PM, Raimon Fs wrote:
> > 
> > > I have one array, and each element of this array, has another array.
> > 
> > \> 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:
> > 
> > values = invoice\_calculs.map do |ic|  
> > ic.attributes  
> > end.map do |a|  
> > a.values  
> > 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:

 a.value for each value

thanks again,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/5 "2007-12-06T22:02:24Z")

</div>

Xavier N. wrote:

> On Dec 6, 2007, at 9:12 PM, Raimon Fs 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

value

thanks,

rai

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/6 "2007-12-06T21:41:05Z")

</div>

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

> 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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/7 "2007-12-06T23:33:39Z")

</div>

Xavier N. wrote:

> 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
> > 
> > value
> 
> Then you are done removing the call to flatten:
> 
> invoice\_calculs.map(&:attributes).map(&:values)
> 
> – fxn

I’m sorry but I don’t understand …

I’m using your first suggestion:

> values = invoice\_calculs.map do |ic|  
> ic.attributes  
> end.map do |a|  
> a.values  
> 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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/8 "2007-12-06T22:35:38Z")

</div>

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
> 
> value

Then you are done removing the call to flatten:

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

– fxn

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 7, 2007, 12:13am UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/9 "2007-12-07T00:13:23Z")

</div>

Xavier N. wrote:

> On Dec 6, 2007, at 11:33 PM, Raimon Fs 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
> 
> [“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:
> 
> [[“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|  
ic.attributes  
end.map do |a|

# 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.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 7, 2007, 1:01am UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/10 "2007-12-07T01:01:05Z")

</div>

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.

– fxn

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 6, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/11 "2007-12-06T23:52:40Z")

</div>

On Dec 6, 2007, at 11:33 PM, Raimon Fs 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

[“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:

[[“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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 7, 2007, 9:06am UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/12 "2007-12-07T09:06:07Z")

</div>

Xavier N. 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:

```
      @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  
attributes:  
base: “707.59”  
tax: “0.0”  
total: “707.59”

## but it’s:

- !ruby/object:InvoicesCalcul  
attributes:  
tax: “0.0”  
total: “707.59”  
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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 7, 2007, 10:18am UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/13 "2007-12-07T10:18:22Z")

</div>

On Dec 7, 2:06 am, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > with tha variable “values” which is assigned the result of the  
> > when I issue the sql:  
> > attributes:  
> > 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|  
ic.attributes  
end.map do |a|  
a.keys.sort.map do |k| # this part  
a[k]  
end  
end.flatten

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 7, 2007, 10:22am UTC](https://www.ruby-forum.com/t/array-iterator-that-have-more-arrays-that-also-need-iteratio/122344/14 "2007-12-07T10:22:41Z")

</div>

Jordan Callicoat wrote:

> On Dec 7, 2:06 am, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > with tha variable “values” which is assigned the result of the  
> > > when I issue the sql:  
> > > attributes:  
> > > 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|  
> ic.attributes  
> end.map do |a|  
> a.keys.sort.map do |k| # this part  
> a[k]  
> end  
> end.flatten
> 
> Regards,  
> Jordan

thanks for the explanation, I see I have to learn more Ruby …

🙂

regards,

raimon
