How to render a two-dimensional array correctly with empty/missing 'cell' elements?

Alex Tweedly alex at tweedly.net
Wed Dec 30 08:56:09 EST 2020


Hi Keith

here's a quick answer typed into email (i.e. not tested at all, sorry)

function displayArray @pArray
   # create variables that point to the line and column of the array
   local tLineItem, tColumnItem
   # create a variable that stores the output for printing
   local tPrintLine
   put empty into tPrintLine
   # loop through each row of the array
   put item 2 of line 1 of the extents of pArray into tNumItem
   repeat with I = 1 to tNumItems
     # loop through each column of the array
     repeat for each key K in pArray[0] -- loop through the tags
       put [pArray[0][K] into tTag
       # write content of the array to tPrintLine
       put pArray[I] & "," after tPrintLine
     end repeat
     # insert a return after each row we have written to tPrintLine
     put return after tPrintLine
   end repeat
   return tPrintLine
end displayArray

Alex

On 30/12/2020 13:48, Keith Clarke via use-livecode wrote:
> Hi folks,
> I’m using LiveCode to parse a set of xml files with varying subsets of tags (dmarc reports) into a CSV file.
>
> The utility iterates through the files in the folder and the lines of the files, to identify unique opening tags and their content, all of which get parsed into an array:
> - Tags go into aDetails[0][tTagName]
> - Values go into aDetails[tFileNumber][tTagName]
>
> This is then displayed as a table, using the function below, borrowed from an LC lesson https://lessons.livecode.com/m/4071/l/12250-how-do-i-store-an-array-variable-inside-of-another-array-variable <https://lessons.livecode.com/m/4071/l/12250-how-do-i-store-an-array-variable-inside-of-another-array-variable>…
>
> function displayArray @pArray
>    # create variables that point to the line and column of the array
>    local tLineItem, tColumnItem
>    # create a variable that stores the output for printing
>    local tPrintLine
>    put empty into tPrintLine
>    # loop through each row of the array
>    repeat for each element tLineItem in pArray
>      # loop through each column of the array
>      repeat for each element tColumnItem in tLineItem
>        # write content of the array to tPrintLine
>        put tColumnItem & "," after tPrintLine
>      end repeat
>    # insert a return after each row we have written to tPrintLine
>    put return after tPrintLine
>    end repeat
>    return tPrintLine
> end displayArray
>
> However, the above function seems to assume that all array elements have content, so I’m getting problems where there are empty elements in the xml files coming from various sources.
>
> I’m sure the loop repeating for each element can be refined to check for ‘empty cells’ by the tTagName key in my array but my LC array knowledge is to sparse to begin the search.
>
> Dos anyone have any tips on where I should start looking?
>
> Thanks & regards,
> Keith
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list