Data Grid Parsing

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Data Grid Parsing

Post by buchacho » Fri Jun 14, 2013 11:07 pm

I am trying to understand how to use data grids, but I did not get to far with the tutorials...

I have a data grid that contains several columns. Let's say I want to:
  • Go down sequentially row by row
    Get the value in the first row and first column and put it into a variable
    Get the character value in the next column
    Then go to another reference table to find a match to that value
    Then pull a value from another column in that row
So in the example below, it would go to the "master grid" grab QWERTY, then find it associated with "3". Then the script would go to the "reference grid", go down the first column, until it found "3" and then grab "pie" and put it into a variable. How can I do this? Thanks!

Master Grid
QWERTY 3
WXYZ 4


Reference Grid
1 Cookie
2 Cake
3 Pie
4 Pudding
Attachments
data grids.png
data grids.png (9.16 KiB) Viewed 11257 times

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Data Grid Parsing

Post by bangkok » Sat Jun 15, 2013 8:37 am

Many ways to do what you want. Here is one simple.

-put the content of the 2 datagrids in 2 variables

put the dgText of group "master grid" into tMaster
put the dgText of group "reference grid" into tReference

-Now, you can, as you say, pick up the first row, and second column for instance

set itemdelimiter to tab
put item 2 of line 1 of tMaster into tChunk

-Now, you can look for tChunk in tReference. Again many ways to do this. Here is one method.

replace cr with tab in tReference
set the wholeMatches to true
put itemOffset(tChunk,tReference) into tResult
if tResult <>0 then
put item (tResult +1) of tReference into tFinish
answer tFinish
end if

This method has flaws. You could use arrays. You could even use SQLlite ;-)

Arrays are very powerfull.

If you put the content of reference in array It would make :
put "COOKIE" into reference[1]
put "CAKE" into reference[2]
put "PIE" into reference[3]

Then, it's very easy to pick up a cell by its number

put reference[tChunk] into tResult

Anyway. Here is a small stack with both methods.
Attachments
LETEST.zip
(5.99 KiB) Downloaded 282 times

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Mon Jun 17, 2013 8:35 pm

Thanks for your reply!

I have another related question. When I try to re-size the columns of my data grid in the property editor, the rendering of the grid is all messed-up and looks jumbled. Why does this happen, and how can I refresh the rendering of the table so that it looks correct? Thanks!

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Passing only selected data from Data Grid

Post by buchacho » Thu Jun 20, 2013 11:15 pm

I have a script that gets the entire data grid an then puts it into an array:

Code: Select all

put the dgData of group "Grid" into GridArray
Now I want to pass the data from only the selected lines of the data grid into the variable (not the entire grid). Is there a simple way of coding that action?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Passing only selected data from Data Grid

Post by Klaus » Fri Jun 21, 2013 11:45 am

HI buchacho,
buchacho wrote:Now I want to pass the data from only the selected lines of the data grid into the variable (not the entire grid).
Is there a simple way of coding that action?
...
## Get all selected INDICES = a COMMA separated list of (line) numbers: 2,5,9,188
put the dghilitedindexes of grp "Grid" into tIndices

## Get complete data:
put the dgData of group "Grid" into GridArray

## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
put GridArray into tFirstSelectedIndexArray
## etc...
## You get the picture
...

Best

Klaus

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Tue Jun 25, 2013 1:22 am

Thanks for the suggestion. It mostly makes sense, but when I try to implement it, it does not work properly. I am thinking the repeat portion is missing something... Does it need to reference tIndices again? And does every column of the row need to be called out and transferred?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Grid Parsing

Post by Klaus » Tue Jun 25, 2013 11:49 am

Hi buchacho,

sorry, I made a mistake in my script:
...
## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
put GridArray[item i of tIndices] into tAnyVariable
...

What exactly do you want to extract form the selected rows?


Best

Klaus

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Tue Jun 25, 2013 7:33 pm

Thanks for the clarification. I would like to copy the complete row (each column) and preserve the column names. I am wondering if this can be done without calling out every single column in the table. Essentially, the exact result of the following code, except only with the highlighted/selected rows of the data grid:

Code: Select all

put the dgData of group "Grid" into GridArray

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Grid Parsing

Post by Klaus » Tue Jun 25, 2013 8:45 pm

Hi buchacho,

ah, i see.

Create a new array containing only the currently selecte rows:

Code: Select all

...
## Get all selected INDICES = a COMMA separated list of (line) numbers: 2,5,9,188
put the dghilitedindexes of grp "Grid" into tIndices

put the dgData of group "Grid" into GridArray
## Now loop through the indices:
repeat with i = 1 to the num of items of tIndices
   put GridArray[item i of tIndices] into tNewArray[i]
end repeat
...
Now tNewArry will contain only the data from the selected rows:
tNewArray[1]["name of your first column"]
tNewArray[1]["name of your seocnd column"]
...
tNewArray[2]["name of your first column"]
tNewArray[2]["name of your seocnd column"]
etc...


Best

Klaus

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Tue Jun 25, 2013 9:03 pm

I do not think this is a valid way of transferring rows between the two arrays since the rest of my script acts as if the resultant tNewArray is empty.

Code: Select all

put GridArray[item i of tIndices] into tNewArray[i]

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Grid Parsing

Post by Klaus » Tue Jun 25, 2013 10:08 pm

Can you please post the rest of your script(s)?

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Wed Jun 26, 2013 1:50 am

Ooops, I think I did not have any lines selected so now it is working.

One more question I have: for a multi-dimensional array, how do I get the number of rows to be able to reference for a repeat function?

repeat with i=1 to the number of rows in tNewArray
...
end repeat

Thanks!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Grid Parsing

Post by Klaus » Wed Jun 26, 2013 10:54 am

Hi buchacho,

do you mean the "keys" of an array?
...
put the num of keys of tNewArray into tNumOfKeys
...


Best

Klaus

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Data Grid Parsing

Post by buchacho » Wed Jun 26, 2013 5:54 pm

Yes, I think so (keys), however, the command results in an error. Does it work with multidimensional arrays?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Grid Parsing

Post by Klaus » Wed Jun 26, 2013 5:59 pm

Hi buchacho,
buchacho wrote:Yes, I think so (keys), however, the command results in an error.
what error? what script do you use?
buchacho wrote:Does it work with multidimensional arrays?
Yes!


Best

Klaus

Post Reply