Check if a deeper subtable tree is valid

Feature requests for future versions of Hollywood can be voiced here
Post Reply
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Check if a deeper subtable tree is valid

Post by jPV »

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:

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"))
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.
User avatar
airsoftsoftwair
Posts: 5446
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Check if a deeper subtable tree is valid

Post by airsoftsoftwair »

jPV wrote: Tue Sep 26, 2023 12:06 pm For some reason even ExitOnError() doesn't seem to work in this case...
Yes, this is because accessing non-existing table fields is a VM error which can't be intercepted by ExitOnError(). Hollywood's error handler can only catch errors not thrown by the VM.
jPV wrote: Tue Sep 26, 2023 12:06 pm 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.
Yes, I see that this could be useful but I don't think there's a reasonable way to implement this. It's not possible to check these things using expressions because non-existing table items will immediately trigger VM errors. So the only way would be to implement a function that takes a string and then parses that string to see if the table exists. But that leads to the problem that you wouldn't be able to use variable indices or expressions, e.g. the following wouldn't work

Code: Select all

DebugPrint(CheckItem("table.list[1].sublist2[n].item"))
So it probably doesn't make too much sense to implement something like this.
Post Reply