Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2065 > unrolled thread
| Started by | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| First post | 2011-03-31 17:26 -0500 |
| Last post | 2011-04-06 00:57 -0500 |
| Articles | 12 — 4 participants |
Back to article view | Back to comp.lang.ruby
Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 17:26 -0500
Re: Simple array.each do |x| question Roger Braun <roger@rogerbraun.net> - 2011-03-31 17:35 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 17:59 -0500
Re: Simple array.each do |x| question Gunther Diemant <g.diemant@gmx.net> - 2011-03-31 18:06 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 18:30 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-01 14:29 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-01 15:31 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-02 01:39 -0500
Re: Simple array.each do |x| question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-02 13:32 -0500
Re: Simple array.each do |x| question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-02 13:58 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-04 13:55 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-06 00:57 -0500
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-03-31 17:26 -0500 |
| Subject | Simple array.each do |x| question |
| Message-ID | <bfa8a96c7e42c61220c2d0d954eca93c@ruby-forum.com> |
Hello, I am new to ruby and cannot understand why this code is not
working:
direction = doc.elements.each("doc:iso_10303_28/uos/IfcCartesianPoint")
{ |element| puts element.attributes["id"] }
# this produces [i1586,i1667,i1915,i2041,i2160]
result = []
direction.each do |z|
n = direction[z].attribute["id"]
result << n
end
This gives me the following error: Error: #<TypeError: C:/Program Files
(x86)/Google/Google SketchUp 8/Plugins/examples/one.rb:61:in `[]': can't
convert REXML::Element into Integer>
However when I use the following code, which to me seems to do exactly
the same thing in a less sophisticated manner, it works without issue:
direction = doc.elements.each("doc:iso_10303_28/uos/IfcCartesianPoint")
{ |element| puts element.attributes["id"] }
n=0
result = []
while n != direction.length
z = direction[n].attributes["id"]
result << z
n=n+1
end
#output - > ["i1586", "i1667", "i1915", "i2041", "i2160"]
Any ideas?
Addition info - Ruby 1.8.6.
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Roger Braun <roger@rogerbraun.net> |
|---|---|
| Date | 2011-03-31 17:35 -0500 |
| Message-ID | <AANLkTimQUjNP75z30+-zYYXD15BZNou6PvEDcic_eFn2@mail.gmail.com> |
| In reply to | #2065 |
Hi
2011/4/1 Kyle X. <haebooty@yahoo.com>:
> Hello, I am new to ruby and cannot understand why this code is not
> working:
>
> direction = doc.elements.each("doc:iso_10303_28/uos/IfcCartesianPoint")
> { |element| puts element.attributes["id"] }
> # this produces [i1586,i1667,i1915,i2041,i2160]
> result = []
> direction.each do |z|
> n = direction[z].attribute["id"]
> result << n
> end
The variable z does not contain an index but the object you need. Try
direction.each do |z|
n = z.attribute["id"]
result << n
end
This should also work:
result = direction.map{|z| z.attribute["id"]}
--
Roger Braun
rbraun.net | humoralpathologie.de
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-03-31 17:59 -0500 |
| Message-ID | <9d19e7d38f2f3b6ab99b3150717a307c@ruby-forum.com> |
| In reply to | #2066 |
Roger Braun wrote in post #990270:
> Hi
>
> 2011/4/1 Kyle X. <haebooty@yahoo.com>:
>> end
> The variable z does not contain an index but the object you need. Try
>
> direction.each do |z|
> n = z.attribute["id"]
> result << n
> end
>
> This should also work:
>
> result = direction.map{|z| z.attribute["id"]}
Thanks for the reply; however, both of these produce a new error is
being produced:
Error: #<ArgumentError: C:/Program Files (x86)/Google/Google SketchUp
8/Plugins/examples/one.rb:62:in `attribute': wrong number of arguments
(0 for 1)>
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Gunther Diemant <g.diemant@gmx.net> |
|---|---|
| Date | 2011-03-31 18:06 -0500 |
| Message-ID | <AANLkTinUfc7WfoXosbPVFG5P7YBgCF8RVAzhVUN9FAAm@mail.gmail.com> |
| In reply to | #2068 |
[Note: parts of this message were removed to make it a legal post.]
attribute is a method, so try z.attribut('id')
2011/4/1 Kyle X. <haebooty@yahoo.com>
> Roger Braun wrote in post #990270:
> > Hi
> >
> > 2011/4/1 Kyle X. <haebooty@yahoo.com>:
> >> end
> > The variable z does not contain an index but the object you need. Try
> >
> > direction.each do |z|
> > n = z.attribute["id"]
> > result << n
> > end
> >
> > This should also work:
> >
> > result = direction.map{|z| z.attribute["id"]}
>
> Thanks for the reply; however, both of these produce a new error is
> being produced:
>
> Error: #<ArgumentError: C:/Program Files (x86)/Google/Google SketchUp
> 8/Plugins/examples/one.rb:62:in `attribute': wrong number of arguments
> (0 for 1)>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-03-31 18:30 -0500 |
| Message-ID | <8517cdac1cada59046c7346685fd2187@ruby-forum.com> |
| In reply to | #2069 |
Gunther Diemant wrote in post #990275:
> attribute is a method, so try z.attribut('id')
>
> 2011/4/1 Kyle X. <haebooty@yahoo.com>
That fixed it, thanks very much for your time and help!
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-04-01 14:29 -0500 |
| Message-ID | <6f6bb08b25ceb72409f83615448a82b6@ruby-forum.com> |
| In reply to | #2069 |
Thank you all for your responses. I have a second part question. I
still cannot get the array.each do || correct, so I have been having to
use these long and unsophisticated while loops:
-------------------------------------
n = 0
elements = []
while n != reference1.length
x =
REXML::XPath.match(doc,"//*[@id='#{reference2[n]}']/Coordinates/IfcLengthMeasure").map
{|element| element.text}
elements << x
n=n+1
end
n = 0
result = []
while n != elements.length
a = 0
while a != elements[n].length
x = elements[n][a].to_f
result << x
a = a+1
end
n=n+1
end
------------------------------------
Any ideas on how to make these more concise by using .each? I have been
trying many different things but cannot make it work. Thank you for
your time.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-04-01 15:31 -0500 |
| Message-ID | <c62e989475c0c5d217bb736ba39e597f@ruby-forum.com> |
| In reply to | #2123 |
Chad Perrin wrote in post #990462:
> On Sat, Apr 02, 2011 at 04:29:13AM +0900, Kyle X. wrote:
>> {|element| element.text}
>> elements << x
>> n=n+1
>> end
>
> I seem to have misplaced the thread so far, so please forgive me if I'm
> asking something redundant.
>
> What is the relationship between reference1 and reference2?
>
I am sorry for not including this. reference1 and reference2 are two
arrays of the same length that contain strings.
If you were to do -
p reference1
#This would be the output->
#["i1671", "i1793", "i1919", "i2045"]
> If they are the same, you should be able to do a very simple swap of
> terms between your while loop version and an each iterator:
>
> elements = []
> reference2.each do |ref|
> x = REXML::XPath.match(
> doc,
> "//*[@id='#{ref}']/Coordinates/IfcLengthMeasure"
> ).map {|element| element.text }
>
> elements << x
> end
>
> (adjusted to reduce horizontal sprawl)
>
> If reference1 and reference2 are not the same length, however, this
> approach will not work. The lack of descriptive meaning in your
> variable
> names is a bit daunting when trying to figure out the intentions behind
> your code.
>
> 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.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-04-02 01:39 -0500 |
| Message-ID | <4ed2ae7acfe8cdab6fbb0fbff47e3e65@ruby-forum.com> |
| In reply to | #2123 |
Chad Perrin wrote in post #990492:
> On Sat, Apr 02, 2011 at 05:31:26AM +0900, Kyle X. wrote:
>> 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:
> On Sat, Apr 02, 2011 at 04:29:13AM +0900, Kyle X. wrote:
>> {|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.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-02 13:32 -0500 |
| Message-ID | <2b98c82b7bbb2ec7c8c0e99b7d0efe84@ruby-forum.com> |
| In reply to | #2148 |
Kyle X. wrote in post #990532:
>
> 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?
>
Yes, and you can always test that. If you use:
p some_array
ruby will output the array structure.
> 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?
>
You wouldn't want to flatten in that case. Instead you can use some
nested map() calls:
data = [[0,1,2],[3,4,5],[6,7,8]]
transformed_data = data.map do |arr|
arr.map do |int|
int.to_f
end
end
p transformed_data
--output:--
[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]
map() sends each element of the data array to the block parameter arr.
Because each element of the data array is a sub-array, I named the block
parameter 'arr' to indicate that. Now that you have a reference to the
sub-array inside the outer block, you can apply another map() to the
subarray to do the actual transformation. The inner map() block returns
a new array that has transformed one of the data sub-arrays to floats.
And the outer map stuffs each of those returned-transformed-arrays into
a new array--producing the final result.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-02 13:58 -0500 |
| Message-ID | <0d447c370de17e3a2a4ee56bde1114ac@ruby-forum.com> |
| In reply to | #2065 |
> This all sounds great but I am a little confused
> with what you exactly mean by, "Feel free to swap out
> do . . . end with { . . . }."
ruby actually has two syntaxes for blocks:
1)
['hello', 'world', 'goodbye'].each {|word| puts word.upcase}
2)
['hello', 'world', 'goodbye'].each do |word|
puts word.upcase
end
The general practice is: use parentheses for one liners, and do-end for
multi line blocks.
There is actually one slight difference in effect between the two
syntaxes, but it is not worth mentioning at this point.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-04-04 13:55 -0500 |
| Message-ID | <c2865a361487ce7e8805b32b9e998dbb@ruby-forum.com> |
| In reply to | #2161 |
Thank you Chris and 7stud, both of your explanations are great and are very helpful. The way you breakdown the examples seems much more clear than in the documentation I find elsewhere which, not having a programing background, can be difficult to digest. Thank you both again very much for your time and help. > And if your coming from perl, or likewise hate typing quotes and commas > for arrays of strings, the above code can be written like this: Unfortunately I am not coming from and programming language background but civil engineering, and for ph.d work in the field apparently you need to know how to program these days.... -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Date | 2011-04-06 00:57 -0500 |
| Message-ID | <3c9acb67e6ad45ef2227596a300c0c39@ruby-forum.com> |
| In reply to | #2065 |
Doh, sorry Chad, just a typo there. Sometimes my mind reads what it wants to read. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web