Chad Perrin wrote in post #990492:
Chad Perrin wrote in post #990462:
>
> I'll come back to the second while loop later, if someone else has
> not already gotten to it by then.
Thank you for the response I will try it out.
I still haven't gotten around to looking at the second loop. I'm
between
articles right now, and I just saw this response -- and wanted to say
that you should make sure you understand the code I provided rather than
copying it directly and never thinking about why it works (or doesn't
work: I can't swear it is perfect, because I did not test it, which is
something I should have mentioned). If you have any questions about how
the code works (or not), please feel free to ask, and I'll explain to
the
best of my ability.
Thank you again Mr. Perrin, I went over the code you provided and
everything seems clear so far. Being new to Ruby the most confusing
thing for me is just learning the new nomenclature, which with all
things takes some time. I have extracted the principle (from your code)
modified slightly and used them on other similar cases, and have not run
into an error in my testing so far.
Chad Perrin wrote in post #990497:
{|element| element.text}
result << x
a = a+1
end
n=n+1
end
------------------------------------
It occurs to me that you're just going through all the elements of an
array within an array here, in order, such that if you have this array:
[[0,1,2],[3,4,5],[6,7,8]]
. . . you're just operating the elements as though the entire data
structure was nothing but a flat array:
[0,1,2,3,4,5,6,7,8]
Keeping that in mind, you could use a basic elements.flatten.each block
instead:
result =
elements.flatten.each {|n| result << n.to_f }
You could use a map/collect block instead, which I think is clearer:
result = elements.flatten.collect {|n| n.to_f }
So once I flatten the array it would become - > [0,1,2,3,4,5,6,7,8] from
[[0,1,2],[3,4,5],[6,7,8]] correct?
This would then allow me to all in one .to_f, yes? Would this be
possible to do without flattening, or is it not possible with arrays in
an array?
I find "collect" a more mnemonically helpful term for this operation
given Ruby's block syntax, though "map" works well for me in Perl. In
Ruby, the terms are interchangeable, so if you really want to type less
you could use "map" instead of "collect" here.
It occurs to me that the solution I gave you last time (assuming it
works) could be combined with this one:
result = reference2.each do |ref|
REXML::XPath.match(
doc,
"//*[@id='#{ref}']/Coordinates/IfcLengthMeasure"
).map {|element| element.text }
end.flatten.map {|n| n.to_f }
Feel free to swap out do . . . end with { . . . } or map with collect as
needed, of course -- or to keep things separate if you think that's
clearer. Ultimately, clarity of intent is probably the most important
factor in deciding how to format your code.
This all sounds great but I am a little confused with what you exactly
mean by, "Feel free to swap out do . . . end with { . . . }."
Thanks for the great information. I kind of figure that there would be
built in commands for the same essential function as I am trying to get
across in my unsophisticated code. Usually when I try to look for the
particular built in function it will take me hours to find (or not
find), and I end up getting discouraged and writing something that is
ugly, unclear and long to just get the job done. It is great to
actually be able to converse with such kind and knowledgeable people as
the people here. Thank you again for all your time and help.
···
On Sat, Apr 02, 2011 at 05:31:26AM +0900, Kyle X. wrote:
On Sat, Apr 02, 2011 at 04:29:13AM +0900, Kyle X. wrote:
--
Posted via http://www.ruby-forum.com/\.