I am in charge of rewriting some old VB code. I understand how it works, but I feel like there is a far-more efficient way to do what they did. I just can't figure out what it is. Here is a contrived example that in terms of data requirements is really similar to what I need to do.
The user has to pick out the manufacturer, make, model and color of their car in a GUI. I have a large text file that looks something like this:
Ford Truck F150 red Ford Truck F150 blue Ford Truck F150 black Ford Truck F150 silver Ford Truck F250 red Ford Truck F250 green Ford Sedan Taurus red Ford Sedan Taurus green Ford Sedan Taurus white Ford... ... Subaru SUV Forester blue Subaru SUV Forester red Subaru SUV Outback Black Subaru SUV Outback Green Subaru SUV Outback Blue Subaru SUV Outback Red Subaru... ... etc.
So if the first selection is Subaru, the second box (make) should not have an option to select Truck because none of the Subarus are trucks. Similarly, if they select Ford, Sedan and Taurus, then the last box (color) should not show an option to select blue. Or Black. Or anything other than red, green or white.
The people who wrote the code before me came up with this (in python-y psuedocode):
def getValidOptions(): items =  for i from 0 to numRows: options = getLine().split() if selectingManufacturer: if options not in items: items.append(options) else if selectingMake: if selectedManufacturer == options and options not in items: items.append(options) else if selectingModel: if selectedManufacturer == options and selectedMake == options and options not in items: items.append(options) else if selectingColor: if selectedManufacturer == options and selectedMake == options and selectedModel == options and options not in items: items.append(options) return items
I think that is just hideous, both on an algorithm level, and on a syntax level. For one, it parses through the entire file, when it only needs to read through a couple of lines if done right. To make this even more inefficient, my real data has 6 options to select through, rather than just 4. This is also storing more data then it needs to, given the amount of data duplication.
I'm looking for either a different way of storing the data in the file, or a different way of parsing it to make the
getValidOptions function both more pretty and more efficient. Are there any ways I could do this?