I'm iterating over a very large set of strings, which iterates over a smaller set of strings. Due to the size, this method takes a while to do, so to speed it up, I'm trying to delete the strings from the smaller set that no longer needs to be used as it goes along. Below is my current code:
Ms::Fasta.foreach(@database) do |entry|
all.each do |set|
if entry.header[1..40].include? set[1] + "|"
startVal = entry.sequence.scan_i(set[0])[0]
if startVal != nil
@locations << [set[0], set[1], startVal, startVal + set[1].length]
all.delete(set)
end
end
end
end
The problem I face is that the easy way, array.delete(string)
, effectively adds a break statement to the inner loop, which messes up the results. The only way I know how to fix this is to do this:
Ms::Fasta.foreach(@database) do |entry|
i = 0
while i < all.length
set = all[i]
if entry.header[1..40].include? set[1] + "|"
startVal = entry.sequence.scan_i(set[0])[0]
if startVal != nil
@locations << [set[0], set[1], startVal, startVal + set[1].length]
all.delete_at(i)
i -= 1
end
end
i += 1
end
end
This feels kind of sloppy to me. Is there a better way to do this?
Ruby | Array delete() operation Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. Syntax: Array. delete() Parameter: obj - specific element to delete Return: last deleted values from the array.
“Iterators” is the object-oriented concept in Ruby. In more simple words, iterators are the methods which are supported by collections(Arrays, Hashes etc.). Collections are the objects which store a group of data members. Ruby iterators return all the elements of a collection one after another.
use delete_if
array.delete_if do |v|
if v.should_be_deleted?
true
else
v.update
false
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With