Arrays, Data Grids & Phantom Records

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
donryan
Posts: 32
Joined: Mon Jul 23, 2012 1:09 pm

Arrays, Data Grids & Phantom Records

Post by donryan » Tue Sep 11, 2012 5:12 am

How do you insert a new record into the first position of an array?

In the openCard script I've got a query that reads sorted records from a database containing 3 fields.
The results are populated into an array.
This array is used by the Data Grid to display records on a form in the Data Grid.
This all works 100%.

What I want to do is to inject a phantom record into the first position of the array before I pass the array to the Data Grid. I do not want to change the actual data itself nor the sort order, I simply want to insert a new record into the position 1 of the array and shift the other records "down" one.

Can anyone give me some guidance on how to accomplish this? :oops:

Thanks!
LiveCode 5.5.3 -- OS X 10.8.2

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Arrays, Data Grids & Phantom Records

Post by snm » Tue Sep 11, 2012 6:15 am

It's pseudocode, I hope should work

put 0 into i
repeat with each key in dataA
add 1 to i
put dataA[i+1] into dataA
end repeat
put something into dataA[1]
populate dataA in DataGrid

Marek

donryan
Posts: 32
Joined: Mon Jul 23, 2012 1:09 pm

Re: Arrays, Data Grids & Phantom Records

Post by donryan » Tue Sep 11, 2012 12:04 pm

Marek:

Thanks for the quick reply -- I will try that later today when I get back to my development system.

So basically, you reposition or move the existing data down 1 in the array (freeing up the first row), then load the new data into the first row of the array. Makes sense ... one more question though:

When the code executes, won't it overwrite the existing data in the array? In other words, if we are taking row 1 and moving it into row 2 -- doesn't that overwrite the existing data that is already in row 2? On the 2nd loop, it would take the new data from row 2 and move it into row 3 (wouldn't that be another copy of the first row)?

If the first row contained "Adam", the second row contained "and", the third row contained "Eve", the code would take "Adam" from the first row and put it into the second row. It would then take the 2nd row, which now contains "Adam" and put it in the third row. When the loop finished, wouldn't the array just consist of copies of row 1 ("Adam", "Adam", "Adam")?

put 0 into i
repeat with each key in dataA
add 1 to i
put dataA[i+1] into dataA
end repeat

Don
LiveCode 5.5.3 -- OS X 10.8.2

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Arrays, Data Grids & Phantom Records

Post by sturgis » Tue Sep 11, 2012 1:22 pm

This should solve the overwriting problem.

Code: Select all

put the number of keys in dataA into tRows -- get the keys
sort tRows descending numeric -- reverse sort them
repeat for each line tLine of tRows -- iterate through the reverse sorted keys
put dataA[tLine] into dataA[tLIne + 1] -- since we're going in reverse, a key is processed before its overwritten.
end repeat
put something into dataA[1] -- add the line to position 1
However, having said this, would it work for you to just use addline to add the line directly to the datagrid after its populated?
AddLine theRowData, theDataColumns, theLineNo (from http://lessons.runrev.com/s/lessons/m/d ... -data-grid)

donryan
Posts: 32
Joined: Mon Jul 23, 2012 1:09 pm

Re: Arrays, Data Grids & Phantom Records

Post by donryan » Tue Sep 11, 2012 1:33 pm

Sturgis:

That's perfect -- I think that solves the problem. The AddLine method will do the trick as well (didn't find that when I looked through the lessons, but that is exactly what I was looking for. Thank you! :D

- Don
LiveCode 5.5.3 -- OS X 10.8.2

Post Reply