Page 1 of 1

Plugin to remove all ADDResses attempt

Posted: 19 Mar 2022 22:33
by jimlad68
I have finally got around to creating a simple Plugin to remove all ADDResses. I know there are workarounds, the Rearrange Address and Place Parts Plugin and simply listing them in a query, selecting then deleteing. But it is about time I at least attempted something simple.

My attempt here is based on the FH plugin Help example Find Date Phrases.
It works, BUT, only does 1 at a time.
I did have a version using ptrItem:MoveToFirstRecord(strType) instead of strTypeDesc which did 2 at a time??

Code: Select all

tblTypes = {'INDI','FAM'} -- Scan both Family and Individual Record Types
ptrItem = fhNewItemPtr()

for iType,strTypeDesc in ipairs(tblTypes) do
    ptrItem:MoveToFirstRecord(strTypeDesc)

    while ptrItem:IsNotNull() do
        strType = fhGetTag(ptrItem)

        if strType == 'ADDR' then -- Search items until ADDR field found
            fhDeleteItem(ptrItem)

        end
        ptrItem:MoveNextSpecial()
    end
end

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 10:32
by tatewise
That is an excellent attempt.
Unfortunately, the ptrItem:MoveNextSpecial() statement is disrupted by the fhDeleteItem(ptrItem) statement that forces the ptrItem value to be null and thus terminates the MoveNextSpecial() loop.

The trick is to add each ADDR ptrItem to a table and then at the end loop through the table deleting items.
e.g.
local tblAddr = {} -- near start

table.insert( tblAddr, ptrItem:Clone() ) -- instead of fhDeleteItem(ptrItem) -- :Clone() is important

for i, ptrAddr in pairs ( tblAddr ) do -- after end
fhDeleteItem(ptrAddr)
end

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 12:57
by jimlad68
Thanks, that works fine. So to overcome the MoveNextSpecial limitation/problem, instead of 1 loop we split into 2 parts.
I did a Notepad++ compare of before and after with no unexpected changes (just 2_ADDR and TIME)

So, next tasks:
- Study the for i, itype, ipairs etc (I suspect a start from scratch looking through the "How to write plugins" and even the linked http://www.lua.org/manual/5.3/, but up to now I cannot find much on itype.
- Look at fhOutputResultSet to see what has been done.

Code: Select all

tblTypes = {'INDI','FAM'} -- Scan both Family and Individual Record Types
local tblAddr = {} 
-- ################## collect ADDResses in table
ptrItem = fhNewItemPtr()
for iType,strTypeDesc in ipairs(tblTypes) do
    ptrItem:MoveToFirstRecord(strTypeDesc)
    while ptrItem:IsNotNull() do
        strType = fhGetTag(ptrItem)
        if strType == 'ADDR' then -- Search items until ADDR field found
            -- fhDeleteItem(ptrItem)
            table.insert( tblAddr, ptrItem:Clone() )
        end
        ptrItem:MoveNextSpecial()
    end
end
-- ################# then delete
for i, ptrAddr in pairs ( tblAddr ) do 
    fhDeleteItem(ptrAddr)
end

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 13:36
by tatewise
Yes, quite often plugins will have several loops or phases to complete the task.

Have you found the FHUG Knowledge Base Family Historian Plugins section.
Especially, Getting Started Writing Plugins and Lua References and Library Modules.

for is a fundamental Lua language looping keyword and ipairs(...) is a standard function found in the language index.

i and iType are simple integer variables that count the loops.

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 14:20
by jimlad68
Yes, thanks for reminding me of those links and simple explanation of the "i" bits. I think doing a simple task "from scratch" that I will find useful has given me more confidence. It is feeling a bit more like my MS VBA "tinkerings" and for me a big step up from basic scripting, now all I need is time.

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 15:00
by Vyger
@jimlad68, may I ask why, origins of Address data, desired goal?

I ask because I still maintain my extensive Address (Place Details) database in Rootsmagic 7, it's an extensive work and too valuable for me to forsake, it remains my last link to the Rootsmagic and will do until CP fill that gap.

Other programs already make value added use of such possible enhanced data so I do believe the day of relational Addresses including geocoding and other attachments will come to FH.

Each to their own but I only ask your reasons as I always think long and hard before knocking something down which took so long to build in my case. The fact FH does not make much use of this valuable data item is certainly no reason for me personally to scrap them so I'm just interested in your reasons?

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 17:22
by jimlad68
Vyger wrote:
20 Mar 2022 15:00
OK, I will give you a past topic link and there are other links from there.
https://www.fhug.org.uk/forum/viewtopic ... 849#p71849

If you don't want to read all that, as regards to ADDResses the jist of it is.
- I keep all my "location" info in the PLACe record, the usual 10 levels separated by commas.
- I use the ADDRess records as an abbreviated version of the PLACe to use in Diagrams where using the full PLACe clutters up the diagram and FH abbreviations do not suit me.
- Hence as part of my ADDR update procedure I have a plugin to abbreviate the PLACe to ADDRess. The reason for my wanting to delete all ADDRs is that the plugin only works with empty ADDRs. I will now hopefully combine the 2 plugins so it is a quick process. If anyone is interesed I will post it later, but very bespoke to my data setup.
- My diagram Text Scheme has an option so that if the ADDR is not there (for say new items) it reverts to PLACe.
See attached.

Re: Plugin to remove all ADDResses attempt

Posted: 20 Mar 2022 20:43
by Vyger
Thank you, we all work in different ways, needs must.