Place Summary Report.fh_lua

--[[
@Title:	Place Summary Report
@Author:	Peter Richmond
@LastUpdated:	28Feb2012
@Version:			1.0
@Description:	Counts and Lists All Places in the File.
	Similar to FH Standard report "Data - Places" but:
	(a) Place parts can be reversed before sorting.
	(b) Count is to the left of the Place.
]]

require("iuplua")
sTitle = "Place Summary Report"
sOption1 = "Normal order (left-to-right)"
sOption2 = "Reversed order (right-to-left)"

iOption = 0
bOK, iOption = iup.GetParam(sTitle, param_action,
"Please specify required Place-part order: %o|"..sOption1.."|"..sOption2.."|\n",
iOption)

if bOK then
	if iOption == 0 then
		sSubtitle = sOption1
		sPlacCol = " Place"
	else
		sSubtitle = sOption2
		sPlacCol = " Place (Parts Reversed)"
	end

	tTypes = {"INDI","FAM"}	-- Record Types that may contain PLAC tags
	tPlaces = {}		-- Define array for Places
	pi = fhNewItemPtr()		-- declare pointer

	for iType,sTypeDesc in ipairs(tTypes) do		-- Scan the two Record Types
		pi:MoveToFirstRecord(sTypeDesc)
		while pi:IsNotNull() do
			sType = fhGetTag(pi)
			if (sType == 'PLAC') or (sType == '_PLAC') then
				sPlac = fhGetValueAsText(pi)
				if (sPlac ~= nil) and (sPlac ~= "") then		-- Process a non-blank Place
					-- Add the Place to the list
					if (tPlaces[sPlac] == nil) then
						tPlaces[sPlac] = 1
					else
						tPlaces[sPlac] = tPlaces[sPlac] + 1
					end
				end
			end
			pi:MoveNextSpecial()
		end
	end

	-- Build Tables for the result set columns for Place and Count
	tPlace = {}
	tCount = {}
	ii = 0		-- line counter for result tables
	iMax = 40		--	inital (minimum) value for length of Place
	for sPlace, iCount in pairs(tPlaces) do
		ii = ii + 1
		tCount[ii] = iCount

		if iOption == 1 then		-- Reverse the order of Place parts
			sRev = ""
			for sPart in string.gmatch(sPlace, "[^,]+") do
				sPart = string.match(sPart, "^%s*(.*)%s*$")
				sRev = sPart .. ", " .. sRev
			end
			sPlace = string.match(sRev, "(.*),%s*$")
		end

		tPlace[ii] = ' ' .. sPlace		-- Leading space to improve readability
		iLen = string.len(tPlace[ii])
		if iLen > iMax then		-- Sets maximum Place length
			iMax = iLen 
		end
	end
	iWid = 4 * iMax		-- Sets Place column width
	fhOutputResultSetTitles(sTitle, sTitle, sSubtitle .. "     %#c")
	fhOutputResultSetColumn('Count', 'integer', tCount,  ii, 24, 'align_right')
	fhOutputResultSetColumn(sPlacCol, 'text', tPlace, ii, iWid, 'align_left', 1)
end

Source:Place-Summary-Report.fh_lua