Tagging table variables within a function argument?

Specifically, table.sort() uses this wonderful gem:

table.sort(data, function(a,b) return a.DPS\>b.DPS end)

Now, I’ve learned earlier (couldn’t for the life of me find this in the official LUA docs, but it’s in the Corona APIs) that you can substitute table categories with variables like this:

local somename = DPS data[i][somename]

But in the table.sort() function above, no such luck. a[somename], a.[somename], and a.somename all fail - the last, I assume, because it’s looking for a literal “variable” category.

So…putting my newbie dunce cap back on, how do you refer to a variable in this sort of situation?

Cheers. [import]uid: 41884 topic_id: 14264 reply_id: 314264[/import]

Hey there,

Have you read this yet? http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

If not, check it out - it’s full of info that could help you :slight_smile:

Peach [import]uid: 52491 topic_id: 14264 reply_id: 52639[/import]

Sadly, not really helpful, though I do enjoy refreshers now and then.

What neither that page nor the two pages it links to covers is the dual [] situation. The blog correctly points out that…

[lua]table.a == table[“a”][/lua]

And I can quickly grasp that taking the “” out of the box would turn ‘a’ from a string into a variable. But none of the articles covers what happens when you need variables for both the ID and the key, eg:

[lua]table[1].key == table[idVariable][keyVariable] – True
table.sort(tablename, function(a,b) return a.Key>b.Key end) – Works
table.sort(tablename, function(a,b) return a[keyVariable]>b[keyVariable] end) – Crashes table [/lua]

As best I can tell from Ansca’s listView code, it should work. But within a function argument the second form is rejected. I’ll check tonight if a[“DPS”]>b[“DPS”] works, (and make some more wild stabs at it, I assure you!) but the original question remains.

[import]uid: 41884 topic_id: 14264 reply_id: 52740[/import]

Not saying much for my reputation, but I’ve got it mostly working now. I really have no idea how, but my best guess is that I may have done something to somehow confuse director.lua.

smh

…Nothing to see here…
[import]uid: 41884 topic_id: 14264 reply_id: 52813[/import]