So far I’ve loaded an entire file into an array (1 line per array element). Then I’ve learned how to search for the correct lines needed. Now I need to split those lines so I can use the data.
1 array element (1 line from file):
data,data,data,data,data
The first data column is text, the others are numbers.
How do I from here split this data into something so I can do math on just one of these columns or plus 2 together, ect ?
As just a secondary note. I have a file with thousands of lines. But I’m picking 20 elements at a time. So I need to do this for 20 elements that are next to each other. So array[56],57,58, ect.
So I can then do math on row 1 column2 – row 3 column 4. Ect. (if this were set up in excel it would be like this – but you get the point – but it can be set up in any way as long as I can know how to get to individual data)
Thanks in advance.
You can just use String::split.
You can then convert the array elements to integers or floats as needed, as in
Additionally, if you are working with CSV files, you might consider using Ruby’s CSV library.