[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array iterator that have more arrays that also need iteratio

Raimon Fs

12/6/2007 7:25:00 PM

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.



<%@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-....

19 Answers

Xavier Noria

12/6/2007 7:45:00 PM

0

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

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

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

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



Raimon Fs

12/6/2007 8:12:00 PM

0

Xavier Noria 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.
>
> <snip>
>> 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
--
Posted via http://www.ruby-....

Raimon Fs

12/6/2007 8:33:00 PM

0

Raimon Fs wrote:
> Xavier Noria 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.
>>
>> <snip>
>>> 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:

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

thanks again,

raimon
--
Posted via http://www.ruby-....

Xavier Noria

12/6/2007 8:40:00 PM

0

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

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

@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


Raimon Fs

12/6/2007 9:02:00 PM

0

Xavier Noria 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
<td>value</td>

thanks,

rai
--
Posted via http://www.ruby-....

Xavier Noria

12/6/2007 9:34:00 PM

0

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>

Then you are done removing the call to flatten:

invoice_calculs.map(&:attributes).map(&:values)

-- fxn


Raimon Fs

12/6/2007 10:34:00 PM

0

Xavier Noria 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
>> <td>value</td>
>
> 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

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

Xavier Noria

12/6/2007 10:52:00 PM

0

On Dec 6, 2007, at 11:33 PM, Raimon Fs wrote:

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

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


Raimon Fs

12/6/2007 11:13:00 PM

0

Xavier Noria 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.
--
Posted via http://www.ruby-....

Xavier Noria

12/7/2007

0

On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:

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

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