* Stop Iterator

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 917
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Stop Iterator

Post by Ron Melby »

embedded is a code snippet I am working on, and have been for two weeks spare time now.

although it is not clear here at this point in the code, I will take in either an INDI ptr, or a SOUR ptr.

trivial to figure out which, can do that with fhGetTAG. INDI ptr needs to find out if a SOUR exists in the record, and then operate on it, if the argument of the parm is source its already linked, straightforward what to do.

perhaps I am using the wrong modifiers here or the wrong functions.

Read this INDI record and this INDI record only
find my source record
return pointer to it as LINK.
else
I dont quite know, return an fhNewItemPtr, so the table indexes are filled out?
end

Please god, make it stop!!!

Code: Select all

function matCEM(iptr)

  local thisPTR  = iptr:Clone()    -- Basing Pointer to INDI
  local ptrSRC   = fhNewItemPtr()
  local ptrLBL   = fhNewItemPtr()
  local ptrLink  = fhNewItemPtr()
  local ptrWork  = fhNewItemPtr()

  local WebPage  = ''
  local MML      = ''
  local DupMML   = ''
  local WebDup   = ''
  local CemAddr  = ''
  local TAG      = '~'
  local rmn      = ''
  local txtlen   = ''

  local function facts()
    ptrLBL:MoveToFirstChildItem(thisPTR)
    while thisPTR and ptrLBL:IsNotNull() do
      ptrLink   = fhGetValueAsLink(ptrLBL) -- cast to same type
      if fhGetTag(ptrLBL) == pType then
        if ptrLink:IsSame(ptrSOUR) then
          ptrWork:MoveToParentItem(ptrSRC)                  -- Basing Pointer to SOUR
          return ptrWork
        end
      end
      ptrLBL:MoveNextSpecial()           --set to next TAG
    end
    return 
  end -- fn facts
  
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28333
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Stop Iterator

Post by tatewise »

I guess that ptrLink:IsSame(ptrSOUR) is a typo and should be ptrLink:IsSame(ptrSRC) ?

ptrSRC is pointing to a record, which by definition has no parent, so
ptrWork:MoveToParentItem(ptrSRC) will always set ptrWork to NULL.
I presume you just need to return ptrSRC

Yes, return fhNewItemPtr() will return the NULL pointer you need for an empty Result Set table entry.

To stop the while loop at the end of current record use:
ptrLBL:MoveNextSpecial()
if not fhHasParentItem(ptrLBL) then break end

That works because when ptrLBL moves to next record it has no parent.

So putting that all together the function becomes:

Code: Select all

  local function facts()
    ptrLBL:MoveToFirstChildItem(thisPTR)
    while thisPTR and ptrLBL:IsNotNull() do
      ptrLink = fhGetValueAsLink(ptrLBL) -- cast to same type
      if fhGetTag(ptrLBL) == pType then
        if ptrLink:IsSame(ptrSRC) then
          return ptrSRC
        end
      end
      ptrLBL:MoveNextSpecial()           --set to next TAG
      if not fhHasParentItem(ptrLBL) then break end
    end 
    return fhNewItemPtr()
  end -- fn facts
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 917
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: Stop Iterator

Post by Ron Melby »

Well here is the thing in its penultimate glory the facts function is now rtvPtrFromINDI()

although I have not guarded any other thng than an INDI or a linked SOUR, I have guarded against a naked SOUR I did it by taking the cowards way out, setting everything to null and chugging merrily along.
I say penultimate, because there is a thing or two I need to work on, and it will be posted separately as non-complex table on the fly. (yes, Virginia, there is a complex table on the fly coming anon, very much later anon anon).


Code: Select all

function matCEM(varptr)
  local thisPTR  = varptr:Clone()    -- Basing Pointer to INDI
  local ptrtype  = ''
  local ptrSRC   = fhNewItemPtr()
  local ptrLBL   = fhNewItemPtr()
  local ptrLink  = fhNewItemPtr()
  local ptrRCD   = fhNewItemPtr()
  local ptrWork  = fhNewItemPtr()

  cwweb  =  cwweb or 0
  cwmml  =  cwmml or 0
  cwwdp  =  cwwdp or 0
  cwgli  =  cwgli or 0

  local WebPage  = ''
  local MML      = ''
  local DupMML   = ''
  local WebDup   = ''
  local CemAddr  = ''
  local OFS      = '~.'
  local rmn      = ''
  local txtlen   = ''

  local function rtvPtrFromSOUR()
    ptrRCD = thisPTR:Clone()            -- Basing Pointer to INDI
    ptrSRC = thisPTR:Clone()             -- Basing Pointer to SOUR
  end -- fn rtvPtrFromSOUR

  local function rtvPtrFromINDI()
    ptrLBL:MoveToFirstChildItem(thisPTR)
    while ptrLBL:IsNotNull() do

      if fhGetTag(ptrLBL) == ptrtype then
        ptrLink = fhGetValueAsLink(ptrLBL)    -- link to this record
        if ptrLink:IsSame(ptrSOUR) then
          ptrRCD = thisPTR:Clone()            -- Basing Pointer to INDI
          ptrSRC = ptrLBL:Clone()             -- Basing Pointer to SOUR
          break 
        end
      end
      ptrLBL:MoveNextSpecial()             -- set to next TAG
      if not fhHasParentItem(ptrLBL) then
        ptrRCD  = thisPTR                 -- Basing Pointer to INDI
        ptrSRC = ptrSOUR                  -- Basing Pointer to SOUR
        ptrLBL:SetNull()                  -- 
        ptrLink:SetNull()                 -- 
        break 
      end
    end 
  end -- fn rtvPtrFromINDI

  ptrtype = fhGetTag(thisPTR)
  if ptrtype == 'SOUR' then
    if not fhHasParentItem(thisPTR) then
      ptrRCD:SetNull()                 -- Basing Pointer to INDI
      ptrSRC:SetNull()                  -- Basing Pointer to SOUR
      ptrLBL:SetNull()                  -- 
      ptrLink:SetNull()                 -- 
    else
      rtvPtrFromSOUR()
    end
  elseif ptrtype == 'INDI' then
    ptrtype = 'SOUR'
    rtvPtrFromINDI()
  end

  ptrRCD:MoveToRecordItem(thisPTR)          -- Basing Pointer to INDI
  ptrWork:MoveToParentItem(ptrSRC)          -- Basing Pointer to SOUR
  --
  -- BURI or CREM  
  rmn = string.sub(fhGetTag(ptrWork), 1, 1)
  table.insert(tblRMN, rmn)

-- Cemetery Addr or disposition
  OFS = OFS .. (fhGetTag(ptrWork))            -- event offset
  CemAddr = (OFS.. '.ADDR')                   -- event address
  ptrWork:MoveTo(ptrRCD, CemAddr)
  if ptrWork:IsNull() then
    CemAddr = (OFS.. '.NOTE2')
    ptrWork:MoveTo(ptrRCD, CemAddr)
  end
  txtlen = fhGetValueAsText(ptrWork)
  cwcem      = math.max(cwcem, #txtlen)
  table.insert(tblCEM, ptrWork:Clone())

-- www.findagrave.com/memorial/1234567890
  ptrWork:MoveTo(ptrSRC,'~.PAGE')
  WebPage = fhGetValueAsText(ptrWork)
  table.insert(tblWEB, WebPage)
  cwweb      = math.max(cwweb, #WebPage)
  table.insert(tblFaG, ptrWork:Clone())

-- memorial number
  MML = WebPage:gsub('.-(%d+).*','%1')
  table.insert(tblMML, MML)
  cwmml = math.max(cwmml, #MML)

-- Note duplicates
  if WebPage ~= MML then
    if tblDup[MML] then
      DupMML = MML
      tblDup[tblDup[MML]] = DupMML
      cwwdp = math.max(cwwdp, #DupMML)
      duplicate = true
    else
      tblDup[MML] = #tblMML
    end
  end
  table.insert(tblDup, WebDup)

-- Plot information
  ptrWork:MoveTo(ptrSRC,'~.NOTE2')
  txtlen = fhGetValueAsText(ptrWork)
  cwgli = math.max(cwgli, #txtlen)                  -- grave lot info
  table.insert(tblPlot, ptrWork:Clone())
  return
end --fn matCem
FH V.6.2.7 Win 10 64 bit
Post Reply