array iterator that have more arrays that also need iteratio

R

Raimon Fs

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
 
X

Xavier Noria

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

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
 
R

Raimon Fs

Xavier said:
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
 
R

Raimon Fs

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
 
X

Xavier Noria

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
 
R

Raimon Fs

Xavier said:
@temp_result is already an array of ARs.

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

@arr.push(@temp_result)

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
 
X

Xavier Noria

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
 
R

Raimon Fs

Xavier said:
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
 
X

Xavier Noria

I'm using your first suggestion:


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
 
R

Raimon Fs

Xavier said:
...
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.
 
X

Xavier Noria

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
 
R

Raimon Fs

Xavier said:
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
 
M

MonkeeSage

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

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
 
R

Raimon Fs

Jordan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top