DataGrid - Looping through all elements, and TSV help

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
anothercoder
Posts: 2
Joined: Sun Nov 22, 2020 9:06 pm

DataGrid - Looping through all elements, and TSV help

Post by anothercoder » Sun Nov 22, 2020 9:21 pm

Hi all-

I am brand new to LiveCode. I've used Python and .NET in the past.

How can I loop through every element in a DataGrid, even if I don't know the column names?

Goal: Make a function to pre-process all cells in TSV that I have loaded into the grid. For example, perhaps I want to check for bad characters or fix formatting.
I want to emphasize it should be generic, ie- I don't know the column names or dimension.

I see this example in the help docs but it assumes I know the columns, which I do not:

Code: Select all

   
   put the dgIndexes of group "DataGrid 1" into theIndexes
   ## Prefix XML
   put "<people>" & cr into theXML
   
   ## Loop through data, putting into XML format
   repeat for each item theIndex in theIndexes
      put "<person>" & cr after theXML
      put "<first_name>" & theDataA[theIndex]["FirstName"] & "</first_name>" & cr after theXML
      put "<last_name>" & theDataA[theIndex]["LastName"] & "</last_name>" & cr after theXML
      put "<title>" & theDataA[theIndex]["Title"] & "</title>" & cr after theXML
      put "</person>" & cr after theXML
   end repeat
   ## Close root XML tag
   put "</people>" after theXML
PS: I'm trying to create a simple app where I import either a CSV or TSV file from Excel and process each cell. I can import the TSV, but LiveCode doesn't seem to process standard TSV conventions. For example, if a TSV cell value is:

Code: Select all

He said "hello" to me.
.. Then Excel saves this, accord to TSV standards, as:

Code: Select all

"He said ""hello"" to me."
But LiveCode's import, from what I see, doesn't deal with these extra formattings... Unless there's a library I'm not aware of?

Thanks for your help, excited to get started with LiveCode.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8322
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: DataGrid - Looping through all elements, and TSV help

Post by FourthWorld » Sun Nov 22, 2020 10:49 pm

Google "CSV must die", and the first hit will provide background on the issue and a LiveCode handler to solve it for you.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

stam
Posts: 196
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: DataGrid - Looping through all elements, and TSV help

Post by stam » Mon Nov 23, 2020 3:27 pm

anothercoder wrote:
Sun Nov 22, 2020 9:21 pm
How can I loop through every element in a DataGrid, even if I don't know the column names?
Hi @anothercoder, here is the code i use in one of my projects -- and BTW i completely agree with 'CSV must die' :)

This code was to import from FileMaker Pro outputs -- many of the field contained flowing text with multiple carriage returns which was causing havoc on import so i cheated a bit and on export from FMP i replaced these with "§"... hey ho ;)
(i should say i used 'Excel' as the export format from FMP as it preserves column names, and then exported the Excel files to TSV, which i used for import into other projects - i initialy did this for a XOJO app but am now using LiveCode for this..)

Code: Select all

function importTSV pPrompt //specific TSVs from FMP, where returns in each field have been replaced with "§"
   local tFile, tArray, tKeys, tNumKeys, uBound, temp, oldItemDelimiter
   answer file "Select the" && pPrompt && "file"
   if it is empty then exit importTSV
   
   put URL ("File:" & it) into tFile
   put the itemDelimiter into oldItemDelimiter
   set the itemDelimiter to tab
   
   put line 1 of tFile into tKeys
   put the number of items of tKeys into tNumKeys
   put line 2 to -1 of tFile into tFile
   put the number of lines of tFile into uBound
   
   repeat with x = 1 to uBound
      repeat with y = 1 to tNumKeys
         put replaceText(item y of line x of tFile, "§", return) into temp
         put replaceText(temp, quote, "") into temp
         put temp into tArray[x][item y of tKeys ]
      end repeat
   end repeat
   
   set the itemDelimiter to oldItemDelimiter
   return tArray
end importTSV
hope that helps a bit...

Stam

stam
Posts: 196
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: DataGrid - Looping through all elements, and TSV help

Post by stam » Mon Nov 23, 2020 3:57 pm

Regarding the example of text containing quotes -- i haven't had text like this, in my cause i just got rid of all quotes as that was my requirement.

But i imagine one way to deal with a field containing quoted text, is that when importing a field of the CSV (within the nested loop), check if first and last char of the field are a quote and put item 2 to -2 of the field into your import variable - in your example it looks like quotes are being escaped as double quotes - you could just replace these with a single quote.

If you do find a helpful library though, do post it here ;)

anothercoder
Posts: 2
Joined: Sun Nov 22, 2020 9:06 pm

Re: DataGrid - Looping through all elements, and TSV help

Post by anothercoder » Wed Nov 25, 2020 8:18 am

Ha ha this is all great, thanks for the help.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8322
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: DataGrid - Looping through all elements, and TSV help

Post by FourthWorld » Wed Nov 25, 2020 2:48 pm

stam wrote:
Mon Nov 23, 2020 3:57 pm
If you do find a helpful library though, do post it here ;)
Follow the guidance in the first reply here and you'll have what you're requesting.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”