Check if a deeper subtable tree is valid
Posted: Tue Sep 26, 2023 12:06 pm
I would like to have a way to check if a deeper subtable tree is valid when trying to check if there's a certain item which is buried under several subtables.
HaveItem() and RawGet() only take one table and one item as inputs, but the problem occurs when the given table isn't valid (some of its parent items doesn't exist).
Here's an example what I mean:
In this case you get an error "Table field "sublist2" was not initialized!" and the script exits. Is there any better way to avoid this than doing HaveItem/RawGet for all those subtables one by one before being able to check existence of the final item?
For some reason even ExitOnError() doesn't seem to work in this case...
So, maybe some kind of extension to the HaveItem() or a whole new function for it? You get these kind of multi level tables all the time when dealing with JSON etc.
HaveItem() and RawGet() only take one table and one item as inputs, but the problem occurs when the given table isn't valid (some of its parent items doesn't exist).
Here's an example what I mean:
Code: Select all
table = {
list = {
{
sublist1 = {
{item = "bla"},
{item = "ble"}
},
sublist2 = {
{item = "bla2"},
{item = "ble2"}
}
},
{
sublist1 = {
{}
}
}
}
}
; Works
DebugPrint(HaveItem(table.list[0].sublist1[0], "item"))
DebugPrint(table.list[0].sublist1[0].item)
; Fails, any better way than check all items one by one in the table tree?
DebugPrint(HaveItem(table.list[1].sublist2[0], "item"))
For some reason even ExitOnError() doesn't seem to work in this case...
So, maybe some kind of extension to the HaveItem() or a whole new function for it? You get these kind of multi level tables all the time when dealing with JSON etc.