From: Chad Perrin Date: 2011-04-02T04:43:34+09:00 Subject: Re: Simple array.each do |x| question --5mCyUwZo2JvN/JJP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Apr 02, 2011 at 04:29:13AM +0900, Kyle X. wrote: > Thank you all for your responses. I have a second part question. I=20 > still cannot get the array.each do || correct, so I have been having to= =20 > use these long and unsophisticated while loops: >=20 > ------------------------------------- > n =3D 0 > elements =3D [] > while n !=3D reference1.length > x =3D=20 > REXML::XPath.match(doc,"//*[@id=3D'#{reference2[n]}']/Coordinates/IfcLeng= thMeasure").map=20 > {|element| element.text} > elements << x > n=3Dn+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? 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 =3D [] reference2.each do |ref| x =3D REXML::XPath.match( doc, "//*[@id=3D'#{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. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --5mCyUwZo2JvN/JJP Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk2WJ/4ACgkQ9mn/Pj01uKUTPACguDM4o7oJPwfTjmwpMubL5npF 2gIAn27oYCq3mfV9TOI/HGkr2zrFTngs =CyVW -----END PGP SIGNATURE----- --5mCyUwZo2JvN/JJP--