big array, problems with deleting all empty items

S

Simon Schuster

it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""
 
R

Robert Klemme

it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""

Your bug is in the loop: you iterate and delete at the same time which
will likely yield strange results (as you observe). Why don't you just do

array.delete_if {|x| x.empty?}

Btw, empty? == true is quite dangerous because in Ruby "true" is not the
only valid value for true.

Apart from that it seems your code is pretty inefficient since you do a
lot copying around. I'd rather do this

ar = []
File.foreach("text.txt") do |line|
line.chomp!
ar << line unless line.empty?
end

Cheers

robert
 
J

James Edward Gray II

it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = File.read("/text.txt").to_a.each { |x| x.chomp! }

That's better written as:

lines = File.readlines("/text.txt")
lines.each { |l| l.chomp! }
080:0> array.each_with_index { |x,y|
array.delete_at(y) if x.empty? == true
}

And this can be as simple as:

lines.delete_if { |l| l.empty? }

However, I would switch strategies and only add the lines you are
interested in to begin with:

lines = Array.new
File.foreach("/text.txt") do |line|
line.chomp!
lines << line unless line.empty?
end

I hope that helps.

James Edward Gray II
 
7

7stud --

Simon said:
it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""

Try running this code:

arr = [10, 20, 30, 40]

arr.each_with_index do |elmt, i|
puts elmt
arr.delete_at(i) if elmt == 20
end

As you can see that code tries to display every element in the array.
Does it succeed?

This convoluted code:
array = File.read("/text.txt").to_a.each { |x| x.chomp! }
array.each_with_index { |x,y|
array.delete_at(y) if x.empty? == true
}

can be done more directly doing something like this:

arr = []

IO.foreach("data.txt") do |line|
if line != "\n"
arr << line.chomp
end
end

puts arr
 
K

Konrad Meyer

--nextPart4871302.aVB6IQtkdz
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Robert Klemme:
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 <[email protected]> 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--
 
M

mortee

Simon said:
077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""

You are modifying the array itself while iterating through it. I haven't
checked specifically how it behaves in your specific case, but this
approach in general needs special attention to have it right in all cases.

I guess you sould check out *#reject instead...

mortee
 
R

Robert Klemme

Quoth Robert Klemme:
it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""
Your bug is in the loop: you iterate and delete at the same time which
will likely yield strange results (as you observe). Why don't you just do

array.delete_if {|x| x.empty?}

Btw, empty? == true is quite dangerous because in Ruby "true" is not the
only valid value for true.

But !!e.empty? == true will do the trick :D.

I've seen the smiley but, frankly, I find this only moderately funny.
The reason is that there are too many people around that think comparing
boolean values is a great idea. This is at least superfluous and often
outright dangerous. Other people then will have to debug those bugs...

Cheers

robert
 
J

John Joyce

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 = 16576, if that could be a potential
problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""
Your bug is in the loop: you iterate and delete at the same time
which will likely yield strange results (as you observe). Why
don't you just do

array.delete_if {|x| x.empty?}

Btw, empty? == true is quite dangerous because in Ruby "true" is
not the only valid value for true.
But !!e.empty? == true will do the trick :D.

I've seen the smiley but, frankly, I find this only moderately
funny. The reason is that there are too many people around that
think comparing boolean values is a great idea. This is at least
superfluous and often outright dangerous. Other people then will
have to debug those bugs...

Cheers

robert
Well, in this case it may be just naive.
In many languages you might have to do something like that.
No big deal.
Comparing a boolean method to a boolean value will only ever return a
boolean.
Can't go wrong.

If it is set up right it could be convenient if there is a chance of
a nil value, because nil == true returns false.
But the condition is still in .empty?
 
S

Stefan Rusterholz

Simon said:
it seems that I have to run it a couple different times for it to
work.. array.length = 16576, if that could be a potential problem. if
it is, how do I work around it?

077:0> array = 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? == true
}

still leaves me with plenty of empty array items
080:0> array[-3]
""

array = File.read(path).split(/\r?\n/)
array.delete!("")

Regards
Stefan
 

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

Forum statistics

Threads
474,269
Messages
2,571,349
Members
48,028
Latest member
Rigor4

Latest Threads

Top