* Not finding all Individuals in a project
-
Langbard
- Gold
- Posts: 18
- Joined: 01 Sep 2014 15:04
- Family Historian: V7
- Location: Worthing, West Sussex
Not finding all Individuals in a project
I've just started experimenting with writing plugins and I'm seeing something strange. The skeleton of my plugin looks like this:
ptrRec:MoveToFirstRecord("INDI")
while not ptrRec:IsNull() do
-- some code here to get name and birth date
ptrCen:MoveTo(ptrRec,"~.CENS")
while not ptrCen:IsNull() do
-- some more code to get census info
ptrCen:MoveNext("SAME_TAG")
end
ptrRec:MoveNext()
end
-- results accumulated above are added to the result set
So it is just one loop inside another and the results are accumulated in tables.
The problem I'm seeing is that I don't seem to be processing all of the people in my project. The sample code given in the help pages reports that there are 848 people, but my result set contains only 303 records. Can anyone see what I am doing wrong?
ptrRec:MoveToFirstRecord("INDI")
while not ptrRec:IsNull() do
-- some code here to get name and birth date
ptrCen:MoveTo(ptrRec,"~.CENS")
while not ptrCen:IsNull() do
-- some more code to get census info
ptrCen:MoveNext("SAME_TAG")
end
ptrRec:MoveNext()
end
-- results accumulated above are added to the result set
So it is just one loop inside another and the results are accumulated in tables.
The problem I'm seeing is that I don't seem to be processing all of the people in my project. The sample code given in the help pages reports that there are 848 people, but my result set contains only 303 records. Can anyone see what I am doing wrong?
- Jane
- Site Admin
- Posts: 8442
- Joined: 01 Nov 2002 15:00
- Family Historian: V7
- Location: Somerset, England
- Contact:
Re: Not finding all Individuals in a project
Without seeing all the code it's tricky to be sure, but I suspect somewhere you are causing ptrRec to be moved or not adding the line to the result set in some circumstances.
I would add a quick counter at the move next record line and see if you have 300 or 800 at the end this will give you a clue.
I would add a quick counter at the move next record line and see if you have 300 or 800 at the end this will give you a clue.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Not finding all Individuals in a project
Since you are creating a Result Set, it also depends on exactly where & how your code is inserting the column table entries. The size of the Result Set is governed by the 4th iCount parameter to fhOutputResultSetColumn.
Remember to use ptrRec:Clone() when adding pointers to Result Set columns.
Perhaps not every Individual has a Census Event.
It sounds like your Plugin is relatively small so you can attach it to your reply using the Upload Attachment tab below the reply edit box, so we can review it all.
One minor comment:
Instead of while not ptrRec:IsNull() you can use while ptrRec:IsNotNull() which is a little clearer.
Remember to use ptrRec:Clone() when adding pointers to Result Set columns.
Perhaps not every Individual has a Census Event.
It sounds like your Plugin is relatively small so you can attach it to your reply using the Upload Attachment tab below the reply edit box, so we can review it all.
One minor comment:
Instead of while not ptrRec:IsNull() you can use while ptrRec:IsNotNull() which is a little clearer.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
-
Langbard
- Gold
- Posts: 18
- Joined: 01 Sep 2014 15:04
- Family Historian: V7
- Location: Worthing, West Sussex
Re: Not finding all Individuals in a project
Hi Mike,
I fixed it, but I don't know why what I was doing wasn't working.
Originally I was using the Record Id as the key for an individual's data in the tables like this:
My theory was that since the Record ID is unique then it would be a good way to identify the data for an individual, even though in the data there are gaps in the Record Id values
I changed the above to:
Where iCount is initially 0 and gets incremented for each INDI record. And this fixed my problem but I don't understand why - I guess it is something I am misunderstanding about using associative arrays in Lua. I had assumed that when I said then that was adding an entry with key 1234 and value "value", but I am now suspecting it was trying to address the 1234th element of the array, which isn't the same thing.
I fixed it, but I don't know why what I was doing wasn't working.
Originally I was using the Record Id as the key for an individual's data in the tables like this:
Code: Select all
iRecordId = fhGetRecordId(ptrRec)
local strNameFirst = fhGetItemText(ptrRec,"~.NAME:GIVEN_ALL")
tblNameFirst[iRecordId] = strNameFirst
tbl1841[iRecordId] = "some census data"I changed the above to:
Code: Select all
iRecordId = fhGetRecordId(ptrRec)
local strNameFirst = fhGetItemText(ptrRec,"~.NAME:GIVEN_ALL")
tblNameFirst[iCount] = strNameFirst
tbl1841[iCount] = "some census data"Code: Select all
tbl[1234] = "value"- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Not finding all Individuals in a project
Your analysis is pretty much correct.
LUA tables are very powerful structures, but have two methods of indexing.
Used as an associative dictionary, any alphanumeric key can hold any value, but the order of the keys is indeterminate.
e.g.
table["apple"] = "green"
table["banana"] = "yellow"
Used as a regular array, any numeric index can hold any value, but ordered by the index.
e.g.
table[1] = "first"
table[321] = "last"
However, an array does NOT have to use contiguous indexes, which can lead to unexpected effects.
When the index is the Record Id you can expect gaps.
Then when fhOutputResultSetColumn() is used it will stop at one of the gaps (not necessarily the first gap).
The above can be demonstrated using the #table operator to get its length.
An associative dictionary always has length zero.
A regular array starting at 1 without gaps has a length equal to its elements.
An array with gaps can have various lengths depending on which gap is chosen by LUA as the end.
TIP:
You do not need a counter to populate a regular array.
Use the table.insert() function instead, which by default appends to the end of a regular array.
e.g.
table.insert(tblNameFirst,strNameFirst)
table.insert(tbl1841,"some census data")
See Tools > Plugins > How to Write Plugins > Introduction to Lua > Lua Quick Guide for more details, and study the Lua Reference Manual.
You can also download and Edit any Plugins from the Store to see how they work, but start with the simpler ones.
LUA tables are very powerful structures, but have two methods of indexing.
Used as an associative dictionary, any alphanumeric key can hold any value, but the order of the keys is indeterminate.
e.g.
table["apple"] = "green"
table["banana"] = "yellow"
Used as a regular array, any numeric index can hold any value, but ordered by the index.
e.g.
table[1] = "first"
table[321] = "last"
However, an array does NOT have to use contiguous indexes, which can lead to unexpected effects.
When the index is the Record Id you can expect gaps.
Then when fhOutputResultSetColumn() is used it will stop at one of the gaps (not necessarily the first gap).
The above can be demonstrated using the #table operator to get its length.
An associative dictionary always has length zero.
A regular array starting at 1 without gaps has a length equal to its elements.
An array with gaps can have various lengths depending on which gap is chosen by LUA as the end.
TIP:
You do not need a counter to populate a regular array.
Use the table.insert() function instead, which by default appends to the end of a regular array.
e.g.
table.insert(tblNameFirst,strNameFirst)
table.insert(tbl1841,"some census data")
See Tools > Plugins > How to Write Plugins > Introduction to Lua > Lua Quick Guide for more details, and study the Lua Reference Manual.
You can also download and Edit any Plugins from the Store to see how they work, but start with the simpler ones.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
- Jane
- Site Admin
- Posts: 8442
- Joined: 01 Nov 2002 15:00
- Family Historian: V7
- Location: Somerset, England
- Contact:
Re: Not finding all Individuals in a project
Also don't forget to check out the plugins:code_snippets:index|code snippets in the Knowledge base
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
-
Langbard
- Gold
- Posts: 18
- Joined: 01 Sep 2014 15:04
- Family Historian: V7
- Location: Worthing, West Sussex
Re: Not finding all Individuals in a project
Thanks Mike, that makes sense. I hadn't noticed but there were gaps in the table that were showing up in the result set. I will update my code to use table.insert as it makes things cleaner.