← Back to Upcase

Searching through a CSV with variable columns

(Sean Knox) #1

I could use some help thinking about a puzzle I’m solving. I was going to tackle it in Ruby, but could be in another language like Javascript/Node. I’m needing help breaking out the problem and designing.

I’m currently working on a command-line program that reads that reads in a CSV, searches the CSV based on the arguments, and then produces output based on what it finds.

The CSV rows have one of two formats. One is simple is a list of restaurants, food items, and their prices:

restaurant ID, price, item label

But for restaurants offering combo meals, where there can be any number of
items in a value meal:

restaurant ID, price, item 1 label, item 2 label, …

So the idea is that you could run this program with the arguments of the CSV file to read and the food items you want to eat, and it outputs the restaurant they should go to, and the total price it will cost them.  It is okay to purchase extra items, as long as the total cost is minimized.

Sample data.csv

1, 4.00, burger
1, 8.00, tofu_log
2, 5.00, burger
2, 6.50, tofu_log

$ foodfinder.rb data.csv burger tofu_log
=> 2, 11.5

Likewise with the rows that have multiple food items:

5, 4.00, extreme_fajita
5, 8.00, fancy_european_water
6, 5.00, fancy_european_water
6, 6.00, extreme_fajita, jalapeno_poppers, extra_salsa

$ foodfinder.rb data.csv fancy_european_water extreme_fajita
=> 6, 11.0

Since data normalization isn’t an option—I can’t shove these into a DB—I was wondering how I might go about thinking about how to parse the CSV in an efficient way. Also that some rows have multiple food items has me unsure how to store those. I’m guessing I’d want to import the rows into a hash and then search through the hash in some fashion. Any guidance, wizards?