From: Konrad Meyer Date: 2007-10-16T03:38:12+09:00 Subject: Re: big array, problems with deleting all empty items --nextPart4871302.aVB6IQtkdz Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Robert Klemme: > On 15.10.2007 19:41, Simon Schuster wrote: > > it seems that I have to run it a couple different times for it to > > work.. array.length =3D 16576, if that could be a potential problem. if > > it is, how do I work around it? > >=20 > > 077:0> array =3D File.read("/text.txt").to_a.each { |x| x.chomp! } > > 080:0> array.each_with_index { |x,y| > > array.delete_at(y) if x.empty? =3D=3D true > > } > >=20 > > still leaves me with plenty of empty array items > > 080:0> array[-3] > > "" >=20 > Your bug is in the loop: you iterate and delete at the same time which=20 > will likely yield strange results (as you observe). Why don't you just do >=20 > array.delete_if {|x| x.empty?} >=20 > Btw, empty? =3D=3D true is quite dangerous because in Ruby "true" is not = the=20 > only valid value for true. But !!e.empty? =3D=3D true will do the trick :D.=20 =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart4871302.aVB6IQtkdz Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHE7OPCHB0oCiR2cwRAr8nAJ4tdnV0xhm2Fi7kPvboV0H6gpvpVgCdFx27 Ukxrw38HuPjQcB02Ibu3eaU= =HVrV -----END PGP SIGNATURE----- --nextPart4871302.aVB6IQtkdz--