Table Search

The only way I’ve found to look up records in a table is to iterate through the table with pairs or ipairs and do comparisons. Many years ago, I worked with some database management systems that would let me index a table on a specific field or fields and do a seek to get to a record.

My table is tiny so this is not a big deal; I just want to make sure I’m not missing something.

thanks,

dave

You could create a table where the key is something that you look for. For instance, if you have a table of names and other info, the key could be the persons last name. Then there’s no lookup, you just access the table record by the persons last name. I don’t know what you have in the table, so I can’t give you a specific example.

@dave1707 The table I’m currently working with is:

    Denominators = {}
        Denominators[1] = {"1", " one ", " ones " }
        Denominators[2] = {"2", " half ", " halves " }
        Denominators[3] = {"3", " third ", " thirds " }
        Denominators[4] = {"4", " fourth ", " fourths " }
        Denominators[5] = {"5", " fifth ", " fifths " }
        Denominators[6] = {"6", " sixth ", " sixths " }
        Denominators[7] = {"7", " seventh ", " sevenths " }
        Denominators[8] = {"8", " eight ", " eights " }
        Denominators[9] = {"9", " ninth ", " ninths " }
        Denominators[10] = {"10", " tenth ", " tenths " }
        Denominators[11] = {"11", " eleventh ", " elevenths " }
        Denominators[12] = {"12", " twelfth ", " twelfths " }
        Denominators[13] = {"13", " thirteenth ", " thirteenths " }
        Denominators[14] = {"15", " fifteenth ", " fifteenths " }
        Denominators[15] = {"16", " sixteenth ", " sixteenth " }
        Denominators[16] = {"17", " seventeenth ", " seventeenths " }
        Denominators[17] = {"18", " eighteenth ", " eighteenths " }
        Denominators[18] = {"19", " nineteenth ", " nineteenths " }
        Denominators[20] = {"20", " twentieth ", " twentieths " }
        Denominators[21] = {"21", " twenty first ", " twenty firsts " }
        Denominators[22] = {"22", " twenty second ", " twenty seconds " }
        Denominators[23] = {"23", " twenty third ", " twenty thirds " }
        Denominators[24] = {"24", " twenty fourth ", " twenty fourths " }
        Denominators[25] = {"32", " thirty second ", " thirty seconds " }
        Denominators[26] = {"50", " fiftieth ", " fiftieths " }
        Denominators[28] = {"64", " sixtty fourth ", " sixty fourths " }
        Denominators[29] = {"100", " hundredth ", " hundredths " }
        Denominators[30] = {"1000", " one thousandth ", " one thousandths " }

I want to look up by the first field in the record.

Thanks for the quick response,

dave

@DaveW - do it like this

Denominators[1] = {" one ", " ones " }
Denominators[2] = {" half ", " halves " }
Denominators[50] = { " fiftieth ", " fiftieths " }

Then you can look up on the numbers (keys) in square brackets

(Table indices don’t have to run in sequence - you can leave gaps).

You could save yourself some typing by only putting in the plural text where it isn’t just adding an “s”.

Denominators[1] = {" one " }
Denominators[2] = {" half ", " halves " } --this is the only one needing a second item
-- etc etc
Denominators[50] = { " fiftieth " }

Then when you look up

txt=Denominators[n] --where n is the number to look up
singularText=txt[1]
pluralText=txt[2] or txt[1] .. "s"

@Ignatz Does it cause problems if I go from Denominators[24] to Denominators[32] in the table?

thanks

Nope, you can leave gaps wherever you like, and it won’t require any extra memory if you do. Lua supports “sparse tables”.

This is because Lua doesn’t actually have numbered arrays. When you write Denominators[1], Lua treats the 1 as being a lookup key rather than a sequence.

So basically, Lua tables contain key-value pairs and are dictionaries rather than arrays.

@Ignatz Excellent! A bad assumption on my part about the numbering.

Thanks much

it’s quite understandable, actually.

Lua tables are unusual and take getting used to (but they are very powerful and I like their flexibility :slight_smile: )

I’d like to know in every tables you suggest to use [n] - Is it a special letter that is meaning something?

The ‘n’ is just a letter. Everyone has letters they use for different things and they have no specific meaning. I use a,b in pairs loop or z in a regular for loop. I also use tab for tables. None of them have anything special about them.

Okay