dot
dot

Reputation: 15660

how do i extract a value from a lua array?

I'm debugging someone else's application and I've run across a data structure that, when dumped to a file, looks like this:

 ["value"] = {}
 ["value"]["0.ouname"] = {}
 ["value"]["0.ouname"]["label"] = "Test value"
 ["value"]["0.ouname"]["seq"] = 90
 ["value"]["0.ouname"]["type"] = "text"
 ["value"]["0.ouname"]["value"] = ""
 ["value"]["1.ouname"] = {}
 ["value"]["localityName"]["value"] = "California"

I need to be able to extract the seq number "90" from it but I'm at a loss as to how. I am able to get at the "California" value by doing the following:

print(myvar.value.localityName.value)

However, I can't seem to get the sequence number

So far, I've tried the following:

print(myvar.value.0.ouname.seq)
print(myvar.value.["0.ouname"].seq)
print(myvar.value."0.ouname".seq)

But I haven't been successful!

If you have any suggestions, I'd appreciate it.

Upvotes: 3

Views: 1014

Answers (2)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

In Lua, a.b is syntactic sugar for a["b"]. Of course, you can use it only when it's possible, and the resulting expression won't mean something else (as in your example).

Thus, you have to use

print(myvar.value["0.ouname"].seq)

You could also drop all dots and use only [], as such:

print(myvar["value"]["0.ouname"]["seq"])

Which is exactly the same format that has been written to file.

Upvotes: 3

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

print(myvar.value['0.ouname'].seq)

Upvotes: 2

Related Questions