I have the following 2 arrays:
fields = ["name", "team", "number", "name", "team", "number", "name", "team", "number"]
values = ["Patrick Ewing", "New York Knicks", 33, "Rik Smits", "Indiana Pacers", 45, "Bill Russell", "Boston Celtics", 6]
The fields will always be only name, team and number, but the number of values (i.e., number of players) will vary each time.
I want to create new arrays for each field type, so that I will get the following:
names = ["Patrick Ewing", "Rik Smits", "Bill Russell"]
teams = ["New York Knicks", "Indiana Pacers", "Boston Celtics"]
numbers = [33,45,6]
What's a good way to do this? I have tried the following, but want to know if there are other solutions that would perform better with larger arrays (up to 300). Or would the difference be negligible?
names = values.values_at(*(fields.each_index.select{ |i| fields[i] == "name"}))
teams = values.values_at(*(fields.each_index.select{ |i| fields[i] == "team"}))
numbers = values.values_at(*(fields.each_index.select{ |i| fields[i] == "number"}))
zip.fieldslist always the same? This is an odd way to organize data on the way in, so if you could adjust that it might require less processing.fieldsalways the same. I do an XML query on a system which returns a bunch of result elements with thefieldsandvaluesas attributes, so I put them intofieldsandvaluesarrays first and go from there.