Is this the correct way of defining 2 persistent variables?

Hi all,

Is this the right way to define two (2) persistent variables?

// Check if nv table exists and if it contains the keys 'count' and 'flag'
if (!("nv" in getroottable()) || !("count" in nv) || !("flag" in nv)) {
// Either the nv table hasn't been instantiated or it has but lacks a 'count' slot or a 'flag' slot
// So create them. If nv table exists, this code just adds 'count' or 'flag' to it. If nv
// doesn't exist yet, this code will automatically create it when it adds 'count' and 'flag'
nv <- { "count": 0 };
nv.flag <- 0;
}

if I do this instead, I get a run time error:
nv <- { “count”: 0 };
nv <- { “flag”: 0 };

Thanks!

If you do this:

nv <- { "count":0 };
nv <- { "flag":0 };

…you shouldn’t get a runtime error, however the nv rable will not have a count entry; you have replaced the entire nv table by one with just a flag entry.

Maybe it’s clearer if you do this:

nv <- { }; // make an empty table
nv.count <- 0; // add count entry
nv.flag <- 0; // add flag entry
1 Like