* fundamental loop

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: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

fundamental loop

Post by Ron Melby »

I am missing something so fundamental here, I am absolutely brain dead from staring and debugging this simple problem:

simple psuedocode:

Code: Select all

get this.INDI
   for FAMS do
     for CHIL do
        count CHIL
    enddo CHIL
  enddo FAMS
end this.INDI
return child count
actual code

Code: Select all

function rtvCHILCount(eptr)   
  local thisPTR = eptr:Clone()
  local ptrFAMS = fhNewItemPtr()
  local ptrFAM  = fhNewItemPtr()
  local ptrCHIL = fhNewItemPtr()

  local fam = 0
  local chil = 0

  ptrFAMS = fhGetItemPtr(thisPTR,'~.FAMS')
  if ptrFAMS:IsNull() then 
    return 0
  end

  while ptrFAMS:IsNotNull() do
    fam = fam + 1
    ptrFAM  = fhGetValueAsLink(ptrFAMS) -- if I use ptrFAM in the moveto below, I run off the page from INDI 
    ptrCHIL:MoveTo(ptrFAMS, '~.CHIL>')  -- if I use ptrFAMS I never find children 
    while ptrCHIL:IsNotNull() do
      chil = chil + 1
      ptrCHIL:MoveNext('SAME_TAG')
    end
    ptrFAMS:MoveNext('SAME_TAG')
  end

  return chil
end 
this has got to be the easiest thing in the world, but it isn't coming to me...
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28414
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: fundamental loop

Post by tatewise »

Change the line near the middle to ptrCHIL:MoveTo(ptrFAM, '~.CHIL')
i.e.
Replace FAMS with FAM because you need the FAM record pointer not the FAMS tag pointer.
Replace CHIL> with CHIL because you need the CHIL tag pointer and not the INDI record pointer.

The reason it loops all INDIviduals is that CHIL> returns an INDI record pointer so the 'SAME_TAG' gets successive INDI records. If you check what is happing when debugging you should see that.

BTW: You don't need the statement below so delete it:
if ptrFAMS:IsNull() then
return 0
end
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Post Reply