* tables again

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

tables again

Post by Ron Melby »

Code: Select all

 tblAREA  = tblAREA or {}
  tblCEM = tblCEM or {}
  local _, gadr, gplc, _, _, _, _ = matGRV(iptr)
  area  = matAREA(gplc)                             -- builds area

  area  = (tblAREA[area] or area)                   -- if new area add
  tblAREA[area] = (tblAREA[area] or 0) + 1
  ainter = (tblAREA[area].ainter or 0 ) + 1         -- increment interments --- ********  attempt to index field '?' (a number value
  tblAREA[area] = {area = area; ainter = ainter;} 

  gadr  = (tblCEM[gadr] or gadr)                    -- if new cemetery add 
  tblCEM[gadr] = (tblCEM[gadr] or 0) + 1
  cinter = (tblCEM[gadr].cinter or 0 ) + 1          -- increment interments
  tblCEM[gadr] = {addr=gadr;  area = area; cinter = cinter;} 
  
what I intend is:
tblAREA[GBR.UK,Kent] area = GBR.UK,Kent, ainter = 27

if I can get the statement right on AREA I will know how to fix tblCEM
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28413
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: tables again

Post by tatewise »

Code: Select all

  tblAREA[area] = tblAREA[area] or {area = area; ainter = 0;}
  tblAREA[area].ainter = tblAREA[area].ainter + 1
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: tables again

Post by Ron Melby »

I have done it again, and dont know how to code this right. this is the code that builds areas and cemeteries (area is from the place record and is transformed, gadr is the address record of the cemetery)I

AREARCD = AREARCD or {}
CEMRCD = CEMRCD or {}
local _, gadr, gplc, _, _, _, _ = matGRV(iptr)
area = matAREA(gplc) -- builds area

AREARCD[area] = AREARCD[area] or {area = area; ainter = 0;}
AREARCD[area].ainter = AREARCD[area].ainter + 1

CEMRCD[gadr] = CEMRCD[gadr] or {addr = gadr; area = area; cinter = 0;}
CEMRCD[gadr].cinter = CEMRCD[gadr].cinter + 1

return
end -- fn matGED

it works properly, I have UNIQUE area and gadr records. I thought it would be a simple matter to change the table keys to integers, so I can sort the tables.

for I=1, #AREARCD do
AREARCD[area] = I
end

for I=1, #CEMRCD do
CEMRCD[gadr] = I
end

these statements do not execute.

as I build them, area and gadr must be unique.
FH V.6.2.7 Win 10 64 bit
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: tables again

Post by Ron Melby »

having figured out a way around that, this is my typical entries for that table:

[288] => (table #1)
[1] => (table .2)
ainter => 2
area => "USA.MN.Todd"
[289] => (table #1)
[1] => (table .2)
ainter => 1
area => "USA.MT.Silver Bow"
[290] => (table #1)
[1] => (table .2)
ainter => 1
area => "USA.WA.Snohomish"
[291] => (table #1)
[1] => (table .2)
ainter => 2
area => "USA.CA.San Diego"
[292] => (table #1)
[1] => (table .2)
ainter => 1
area => "USA.MN.Big Stone"
[293] => (table #1)
[1] => (table .2)
ainter => 80
area => "USA.MN.Olmsted"

i have inspected each of the 293 records, and area is not nil in any record.

however at the very first record of

table.sort(AREARCD, AREASORT)

function AREASORT(a, b)
return a.area < b.area -- line 61
end

i get:
61: attempt to compare two nil values

a => (table #1)
[1] => (table .2)
ainter => 80
area => "USA.MN.Olmsted"
b => (table #1)
[1] => (table .2)
ainter => 2
area => "USA.OK.Murray"


how can this possibly be true?
FH V.6.2.7 Win 10 64 bit
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

yRe: tables again

Post by Ron Melby »

well geeze this seems like a long way around the barn:

inexist = false
for _, v in ipairs(AREARCD) do
if area == v.area then
v.ainter = v.ainter + 1
inexist = true
end
end
if inexist == false then
AREARCD[#AREARCD + 1] = {area = area; ainter = 0;}
AREARCD[#AREARCD].ainter = AREARCD[#AREARCD].ainter + 1
end

inexist = false
for _, v in ipairs(CEMRCD) do
if gadr == v.addr then
v.cinter = v.cinter + 1
inexist = true
end
end
if inexist == false then
CEMRCD[#CEMRCD + 1] = {addr = gadr; area = area; cinter = 0;}
CEMRCD[#CEMRCD].cinter = CEMRCD[#CEMRCD].cinter + 1
end

^^^^ that is working code.... Mike, you can prolly do that in -7 statements and 50 times faster than mine.....lol. best I can come up with though

why wont this work, or am I still incorrect in statements?
I believe I am simply changing all keys from what they are to integers in the new table, and bringing any values along

function genINDEX(tbl)
local INDEX = {}
for _, flds in pairs(tbl) do
table.insert(INDEX, {flds})
end
return INDEX
end

I see the problem -- darkly, but dont see how to move the field values (not knowing what they are, but doing it in general) without the {}... select ('#') didnt work, but maybe I am not using it correctly.
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28413
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: tables again

Post by tatewise »

I have not got the time to wade through all that code.
You keep forgetting that it is only possible to sort numerically indexed tables (that I call arrays).
Also for I=1, #AREARCD do or for I, V in ipairs (AREARCD) do only works for numerically indexed tables.

Your tables are indexed by text (that I call dictionaries) so table.sort and #AREARCD and ipairs won't work.

So you need to compose a combined dictionary and array table:

Code: Select all

  if not tblAREA[area] then
    tblAREA[area] = { area = area; ainter = 1; }    -- Dictionary with textual area index
    table.insert( tblAREA, area )    -- Array with numerical index that refers to dictionary entry
  else
    tblAREA[area].ainter = tblAREA[area].ainter + 1    -- Increment count for repeated area
  end
Now table.sort(tblAREA) will sort the numerical indexes into area order.

Then to process in area order use:

Code: Select all

  for index, area in ipairs (tblAREA) do    -- Loop through array using numerical index
    local tblArea = tblAREA[area]    -- Look up dictionary values for area and ainter
  end
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
ColeValleyGirl
Megastar
Posts: 5502
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: tables again

Post by ColeValleyGirl »

Penlight, again. I know i keep suggesting Penlight, but it could save everybody an abundance of tears and time:

https://stevedonovan.github.io/Penlight ... html#sortv

returns an iterator to a table sorted by its values

and https://stevedonovan.github.io/Penlight ... .html#sort

returns an iterator to a table sorted by its keys

I know Ron has found the documentation opaque, but yu might want to take a look, Mike.
User avatar
tatewise
Megastar
Posts: 28413
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: tables again

Post by tatewise »

Yes, those Penlight functions offer handy shortcuts to the sorting process.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: tables again

Post by Ron Melby »

while I did find the way to replace the keys, after a small struggle with the level breaks I have decided to do it another way, in which they need to retain their current keys. my problem is now in a different area, described in (and solved in) where is the error?

however the sort and sortv are going to have some use coming up.
FH V.6.2.7 Win 10 64 bit
Post Reply