How to reference first entry in keyless table in Lua?

39 views Asked by At

I have a table defined like so:

local foo =
{
    bar =
    {
        { ['baz'] = "A sample string", ['qux'] = 128 },
    },
}

Obviously, the is a small part of a much larger algorithm. I'm having trouble referencing the first entry in table bar.

print("foo.bar", "type", type(foo.bar), "value", foo.bar)
print("foo.bar.first", "type", type(foo.bar[0]), "value", foo.bar[0])

If I try a simple print, like shown above, the second print outputs nil for type and value.

foo.bar type    table   value   table: 000001DF39D19D20
foo.bar.first   type    nil value   nil

FYI, if I put it in a loop using the pairs() or ipairs() functions, I can get at the baz and qux members without any problem, but I'd prefer not to us a loop here. Is this possible? How may I reference the first entry in a keyless table without looping?

0

There are 0 answers