Page 1 of 1

how to get #entries.

Posted: 08 Feb 2019 18:36
by Ron Melby
so, I am trying to determine, (since it should be non-nil if it exists)
how do I get the NAME[ ], SPOU[], MARR[] and so on, number of entries?

nent = #NAME
sent = #SPOU
ment = #MARR

That isnt going to work, I know that before I even test it.

Re: how to get #entries.

Posted: 08 Feb 2019 19:32
by tatewise
You are correct the # length operator only applies to Lua data items and not GEDCOM structures.
Unfortunately, you have to count them using a while loop.
e.g.

Code: Select all

local ptrIndi = fhNewItemPtr()
ptrIndi:MoveToFirstRecord("INDI")
while ptrIndi:IsNotNull() do
	local intName = 0
	local ptrName = fhNewItemPtr()
	ptrName:MoveTo(ptrIndi,"~.NAME")
	while ptrName:IsNotNull() do
		intName = intName + 1
		ptrName:MoveNext("SAME_TAG")
	end
	print(intName)
	ptrIndi:MoveNext()
end
You could make the counting loop a function with parameter (ptrItem) and return the count.
e.g.

Code: Select all

local function intInstances(ptrItem)	-- Count number of instances of Item
	local intItem = 0
	while ptrItem:IsNotNull() do
		intItem = intItem + 1
		ptrItem:MoveNext("SAME_TAG")
	end
	return intItem
end -- local function intInstances

local ptrIndi = fhNewItemPtr()
ptrIndi:MoveToFirstRecord("INDI")
while ptrIndi:IsNotNull() do
	local intName = intInstances( fhGetItemPtr(ptrIndi,"~.NAME") )
	print(intName)	-- Diagnostic printout of instances
	ptrIndi:MoveNext()
end
Then you can use function intInstances to count any data item instances.

Re: how to get #entries.

Posted: 08 Feb 2019 22:03
by Ron Melby
thats sort of what my code looks like

I thought first #NAME, realized while it looks ok, its just a text string, and wont work
then I thought about pairs, but if there is a table behind it I cant get at it.

strDataOffset = "~.NAME[" .. ln .. "]:SURNAME_FIRST"
is what I ended up with

where ln is the last name in the structure
and it works fairly well for what I want, but doesnt account for non-marraiges, husbands I dont know, and children out of wedlock..but I can work with it.

Re: how to get #entries.

Posted: 08 Feb 2019 22:09
by Ron Melby
sideways a little from the subject, but still this plugin, so if it needs moving...

reading what I can find of the spec: require can be anywhere in the file, is that correct?

also, I have been trying to come to grips with coroutines and fhUpdateDisplay() trying to find a way to update the output tables when I make a change from the screen, and do not think it is possible, or am I missing something?

Re: how to get #entries.

Posted: 08 Feb 2019 22:34
by tatewise
I think require needs to executed before the components of the module are called.
Usually the require is invoked at the very start of the Plugin. Why put it anywhere else?

coroutines are quite tricky, especially since you still seem to be struggling to grasp more basic concepts.
How were you hoping to use coroutines?

fhUpdateDisplay() only applies to windows that were on display in FH before the Plugin was run.
It cannot possibly apply to the Result Set output because that does not exist until after the Plugin has completed.

Re: how to get #entries.

Posted: 08 Feb 2019 22:38
by Ron Melby
then what I want doesnt appear to be doable.