* array inside an array

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

array inside an array

Post by Ron Melby » 06 Aug 2022 01:22

famOBJ[#famOBJ + 1] =
{
fID = _cs.fID,
FAM = FAM,
MDAT = _cs.MDAT,
DDAT = _cs.DDAT,
HUSB = HUSB,
WIFE = WIFE,
CHIL = {},
}

local lnkCHIL = fhNewItemPtr()
local ptrCHIL = fhNewItemPtr()
lnkCHIL = fhGetItemPtr(ptrFAM, '~.CHIL')
while lnkCHIL:IsNotNull() do
ptrCHIL = fhGetValueAsLink(lnkCHIL)
local chil = fhGetRecordId(ptrCHIL)
famOBJ[#famOBJ][CHIL][#CHIL + 1] = chil line 18 (235 in real code)

235: attempt to get length of global 'CHIL' (a nil value)
the same result happens for:
famOBJ[#famOBJ]['CHIL'][#CHIL + 1] = chil
famOBJ[#famOBJ].CHIL[#CHIL + 1] = chil

I have built famOBJ at 1 through 10

famOBJ => (table #1)
[1] => (table .7)
MDAT => ""
DDAT => ""
CHIL => (table .0)
FAM => "Covey, John William"
WIFE => ""
fID => 1849
HUSB => 4464

I intend to put that familys fid:[1849] children indi records in CHIL
clearly (in my mind am specifying the CHIL array that I want to build in famOBJ, and believe I am addressing it statement 235

there is some obvious and trivial error afoot here, but it escapes me. Who sees the problem?

thanks
FH V.6.2.7 Win 10 64 bit

User avatar
Jane
Site Admin
Posts: 8354
Joined: 01 Nov 2002 15:00
Family Historian: V7
Location: Somerset, England
Contact:

Re: array inside an array

Post by Jane » 06 Aug 2022 08:39

CHIL does not exist as an object in it's own right it is a sub field of famObj so you need to access the # count as such.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."

User avatar
tatewise
Megastar
Posts: 24655
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: array inside an array

Post by tatewise » 06 Aug 2022 10:25

There are several problems with the script you supplied that prevents it from running and hinders debugging.
e.g. There is no definition of famOBJ or ptrFAM or _cs before their first use.

However, ignoring those issues, the simplest solution to line 235 is:
table.insert( famOBJ[#famOBJ].CHIL, chil )
which will automatically handle the numerical indexing far better than using # counts.

If you are determined to use # counts instead of table.insert(...) then line 235 is:
famOBJ[#famOBJ].CHIL[ #famOBJ[#famOBJ].CHIL+1 ] = chil
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

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

Re: array inside an array

Post by Ron Melby » 06 Aug 2022 12:25

Mike, it works, thank you. I am astounded.

The reason that I use inserts in the #, # + 1 way is that they are lightning faster than table.insert and this is a subprogram of a really intense program anyway.

in either case, you provided, I would not have come to that on my own. the statement has a familiar quality, darkly, but when I plug in the concrete terms instead of the # terms in my head, I dont quite get it, I will have to spend a great deal of time thinking on this. it is like the first time you run across negative negative a =- a (--a = a) in maths. one can develop several scenarios in their head why that is so, but cannot quite explain it to someone else, and finally just accepts it as a truth.

one reason I did not supply the whole program, is that it is several requires to run it, and the code would run for pages and pages. famOBJ is a 'local' global array at top of program, and _cs. is from a require that returns values from a code segment, like matNAME, matfID, matERA, and so on.
FH V.6.2.7 Win 10 64 bit

User avatar
tatewise
Megastar
Posts: 24655
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: array inside an array

Post by tatewise » 06 Aug 2022 13:34

Ron, I was not asking for the whole program, just a snippet of code that runs to illustrate your problem.

To use the # counts you must supply the entire object that needs counting.
i.e.
As Jane said CHIL does not exist in its own right. It is a sub-array of famOBJ so you must start there.
The particular count you want belongs to a particular indexed entry in famOBJ which is famOBJ[#famOBJ]
So the item you want to count is famOBJ[#famOBJ].CHIL so local count = #famOBJ[#famOBJ].CHIL
and thus famOBJ[#famOBJ].CHIL[ count+1 ] = chil does what you want.
I simply avoided the temporary intermediate count value.

Maybe the way to go is to use table.insert(...) to get the logic working correctly with the nested table structure.
Then focus on replacing table.insert(...) with the # count alternative to gain speed.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

Post Reply