r/learnruby • u/xphaserx • Jan 13 '15
Help with getting individual values from csv
Hi guys,
I'm very new to Ruby and I need to use it for building a framework for testing.
While I'm able to get down some of the basics, I've recently hit a wall: my current objective is to create a csv (or any spreadsheet that will get the job done) that contains values.
The purpose is to have my Ruby script retrieve the values from the csv instead of changing the values within the script. The documentations that I've come across only mention retrieving the values at rows at a time but I want to specifically pinpoint the individual cell (row, column).
Can you guys point me in the right direction in how to make this happen?
I hope I'm explaining this right and your help is greatly appreciated!
View all comments
Show parent comment
2
u/science_robot Jan 13 '15
If you don't mind reading in the CSV every time; the easiest way to do this would to use a Hash of hashes:
{ row_id: { column_id: cell_value } }.You can't do this without reading the entire file every time unless you either (a) fix the byte-length of the cell values (and don't add any columns) and (b) index the row and columns IDs. So you would go
row_id->col_id->byte position of cell.