13 Jan, 2013, yue wrote in the 1st comment:
Votes: 0
Hiya.

I have a script that pulls informations from text with regular expressions. The regexps capture certain data from given lines and save the data (as a table) in specific subtables of another table. It looks like this.

mainTable = {}
mainTable.firstsub = {}
mainTable.secondsub = {}
mainTable.thirdsub = {}

and so on

The saves look like this: (added with table.insert(mainTable.<specificsubtable>, tableOfDataToSave)
mainTable.firstSub[1] –> {argument1="some text",argument2="someothertext",argument3=numbers}
mainTable.secondsub.[1] –> {differentargument="sometext", differentargument=number}
etc..


I can display all entries of a specific subtable, something like display(mainTable.subtable) which is useful in itself but lacking. What I need is an iterator func that can return specific results. Like listing every key/value pair a specific subtable that contains argument2="specifictext". I have been trying to use table.contains() or a for loop, or next,value,nil, or a mix-mash of the three to accomplish this, with no success.

Any suggestions on how to go about this. My understanding of for loops just isn't there. I can iterate through mainTable.subtable and return results, but it is usually a mem address of the subtable. That's not what I want.
14 Jan, 2013, yue wrote in the 2nd comment:
Votes: 0
how does:

for k,v in ipairs(table.subtable) do
if table.contains(v, "what I am looking for") then
display(v)
end
end


look?
14 Jan, 2013, Runter wrote in the 3rd comment:
Votes: 0
In general, yes, your solution works.

Keep in mind the point of associative arrays is to allow you to quickly lookup values given a key. In your example, actually, "what I am looking for" is a key.

So depending on your specific use case and how often you're doing a lookup, you very well may decide to make it a key rather than a value. You also aren't limited to a single associative array. It's possible to map values to their key. For example looking up "your value" using it as a key in a table that returns an array of keys to another table, where the values are "your value." Needless to say, the complexity of having non-unique values is mitigated by just iterating over the entire thing like you did in your example, but there are times where the tables may be very large.
14 Jan, 2013, yue wrote in the 4th comment:
Votes: 0
My table is VERY large.

itemPrices.bombs –> a table
itemPrices.booze –> a table
itemPrices.guns. –> a table

itemPrices.guns[1] –> another table

display(itemPrices.guns[1]) –> {name="Uzi", shop="The Gun Shop", price=500}

display(itemPrices.booze[11] –> {name="Yue's IPA", shop="WSBC", price=10, type="Refill from Keg"}

etc…


What I'm trying to accomplish is item lookups, listing items by shop, or by name, or say, all booze under the price of 12.
14 Jan, 2013, yue wrote in the 5th comment:
Votes: 0
I read your post. Then I read it again. Then I had a cigarette and thought about it.

What you're saying is I should restructure my table? I'm just not sure what would be a more elegant approach.
0.0/5