I'm trying to read numbers from a txt file into an array in ruby with some conditions. The text file input looks like this:
1 2 3 4 5 6 7 8 9;10 11 12
What I want to do is read the numbers up to ';', store them in array, execute a method on that array, and then clear the array and start reading from the semicolon again. Here you can see the execution of the program with the input seen above:
read [1,2,3,4,5,6,7,8,9] -> Execute method X -> [] Clear array -> [10 11 12] -> execute method X...
I think a variation of the following code would do the trick, but I don't know enough ruby to do this myself.
a = []
File.open('names.txt') do |f|
f.each_line do |line|
a << line.split.map(&:to_i)
end
end
Thanks for your help! This is for curiosity, not an assignment or anything else. I'm trying to build ruby skills by doing simple things.