2

I have some arrays that I put into one array called alist[]. I am iterating over the array to print out all values of alist[]. I need to find a value at alist[2][i] and then remove the alist[0][i], alist[1][i], alist[2][i], alist[3][i], alist[4][i] from my array alist[][]. (I removed the code that fills my arrays so it is easier to read my question)

This is my best guess below but it is not working. Would anyone have any ideas?

#declare arrays
nsymbol = []
sname = []
etf = []
testv = []
financials = []
alist = []

#create one array with all other arrays
alist.push(nsymbol, sname, etf, testv, financials)

(0...nsymbol.length).each do |i|
  (0...alist.length).each do |j|
    if (alist[2][i] || '').include? 'Y'
      alist.delete_at(0)
      alist.delete_at(1)
      alist.delete_at(2)
      alist.delete_at(3)
      alist.delete_at(4)
    end
    #print whole array out
    puts alist[j][i]
  end
end

1 Answer 1

3

By performing alist.delete_at(0) you delete the first item of alist so to speak alist[0][0..N] but you want to delete alist[0][i] so you need to delete the ith item of alist[0].

alist[0].delete_at(i)
alist[1].delete_at(i)
# etc.

Because you are printing your array just after deleting the new contents it doesn't matter, but if you want to use the array after this you should break the loop after deleting the entries, because deleting the entries leads to another entry now being the item alist[2][i] and eventually to a further deletion of the entries. (Though this might also be exactly what you want).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is exactly what I was looking for. Then I'm gonna dump alist[] into another array that will be the correct size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.