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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Rick Harrison via use-livecode
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
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

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

Rick Harrison via use-livecode
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
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

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

Rick Harrison via use-livecode
Hi Alex,
Thanks for the response and suggested code - the change from iterating through array column items in each row to the header row[0] keys was the enlightenment I needed... “Eureka!" :)

A couple of tweaks and it’s working very nicely!

function displayArray @pArray

local tLineItem, tTag -- variables for array line and column (headers)

local tPrintLine -- variable for output printing

put empty into tPrintLine


# loop through each row of the array

put item 2 of line 1 of the extents of pArray into tNumItems

repeat with I = 0 to tNumItems -- row 0 contains column labels

# loop through each column for each row

repeat for each key K in pArray[0]

put K into tTag

put pArray[I][tTag] & "," after tPrintLine -- write content of the row ‘cell' to tPrintLine

end repeat

put return after tPrintLine -- insert a return after each row we have written to tPrintLine

end repeat


return tPrintLine

end displayArray


Thanks & regards,
Keith

> On 30 Dec 2020, at 13:56, Alex Tweedly via use-livecode <[hidden email]> wrote:
>
> 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
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode