* unnamed table variables

Writing and using plugins for Version 5 and above.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 635
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

unnamed table variables

Post by Ron Melby » 22 Oct 2020 05:39

local _cs =
{
fID = _rv.fID,
FAM = FAM,
MDAT = (_rv.fmdat:GetDisplayText('COMPACT') or ''),
ptrHUSB = lnkHUSB:Clone(),
hID = fhGetRecordId(ptrHUSB),
HNAM = HUSB,
ptrWIFE = lnkWIFE:Clone(),
wID = fhGetRecordId(ptrWIFE),
WNAM = WIFE,
CHIL = CHIL
}

this is a concrete chunk for a table entry.
I would like to save this and other tables.
is there a general way I can go thru the entries in an array in a table
for k, v in pairs (tbl) do
for ?fld, ?nflds tbl[#tbl] do
if type(?fld) == 'userdata' then
?fld == nil
end
end
to clear all userdata from the table?
so that it saves.
--not knowing the name of the field
if gedrcd[g
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 19155
Joined: 25 May 2010 11:00
Family Historian: V6.2
Location: Torbay, Devon, UK
Contact:

Re: unnamed table variables

Post by tatewise » 22 Oct 2020 10:02

Yes, that is exactly what the type(...) function does.

Code: Select all

for k, v in pairs (tbl) do
  if type(v) == 'userdata' then
    tbl[k] = nil
  end
end
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Ron Melby
Megastar
Posts: 635
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: unnamed table variables

Post by Ron Melby » 22 Oct 2020 12:41

function crtSaveable(tbl)
for k, v in pairs (tbl) do
_a = type(v)
if type(v) == 'userdata' then
tbl[k] = nil <<** do not want to kill the entire entry, just fields in the entry so if I get there this changes somehow.
end
end
return tbl
end -- fn crtSaveable

_a = 'table'
and I have the entire entry in v , not an individual field.


v => (table .17)
ERX => " ( - ) "
NAME => "KNUDSEN, (OLSDATTER) Adstri"
RLT => "Great (x06) Grand mother"
SEX => "F"
fdptr (item) => (null)
BIN => "100000001"
GEN => 8
AHN => 257
DEATH => " "
AGE => 0
ERA => "( - )"
fID => 0
iPTR (item) => Individual: Adstri OLSDATTER
BIRTH => " "
iID => 5266
fmdat (date) => (null)
PTG => 2

once I have v, I would like to loop thru all the fields in v individually looking for userdata.
I do not know the names of the fields or how many. some fields may be arrays themselves, but if I cannot address a simple unknown field, that is down the line.
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 19155
Joined: 25 May 2010 11:00
Family Historian: V6.2
Location: Torbay, Devon, UK
Contact:

Re: unnamed table variables

Post by tatewise » 22 Oct 2020 13:20

Your concrete example did not suggest anything about nested tables.
However, simply nest the solution recursively.

Code: Select all

function crtSaveable(tbl)
  for k, v in pairs (tbl) do
    if type(v) == 'userdata' then
      tbl[k] = nil
    elseif type(v) == 'table' then
      crtSaveable(v)
    end
  end
end -- fn crtSaveable
You should be able to work that out for yourself.

BTW: You don't need to return tbl as it is passed by reference.
So the original table will have all its 'userdata' entries deleted.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Ron Melby
Megastar
Posts: 635
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: unnamed table variables

Post by Ron Melby » 22 Oct 2020 13:42

I did not think of doing it recursively, rather in place loop. thanks, that makes sense.

nevertheless, I do not want to kill the table entry, just pointer fields in the table entry.
for instance:
iPTR (item) => Individual: Adstri OLSDATTER
needs to be nil (unless that is also userdata
and I suppose dates need to be nill as well as date points.

for k.[v] [1] to k[v][999999999] do (cannot take #v on v tried it]
if k.[v][nbr] = 'userdata' then
k.[v][nbr] == nil
end
end
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 19155
Joined: 25 May 2010 11:00
Family Historian: V6.2
Location: Torbay, Devon, UK
Contact:

Re: unnamed table variables

Post by tatewise » 22 Oct 2020 14:14

Yes, my solution does exactly what you asked for. Have you tried running it? I suspect not.
It removes table items that are 'userdata' and keeps all the others at each and every level of nested tables.
It does NOT kill whole table entries.
In what way does my solution not work for you?

As is too often the case your psuedo code makes no sense to me.
We know for x = 1, #tbl do only works for numeric arrays, and is why we use for k, v in pairs (tbl) do

All FH pointers, dates, date points are 'userdata'.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
Ron Melby
Megastar
Posts: 635
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: unnamed table variables

Post by Ron Melby » 23 Oct 2020 14:03

I finally got that working, problems in code beforehand that would not place certain fields in an entry, so as I watched the results it looked like more was missing than should have been. As you said, it works perfectly. Thanks.
FH V.6.2.7 Win 10 64 bit

Post Reply