Page 1 of 1
Arrays, Data Grids & Phantom Records
Posted: Tue Sep 11, 2012 5:12 am
by donryan
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?
Thanks!
Re: Arrays, Data Grids & Phantom Records
Posted: Tue Sep 11, 2012 6:15 am
by snm
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
Re: Arrays, Data Grids & Phantom Records
Posted: Tue Sep 11, 2012 12:04 pm
by donryan
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
Re: Arrays, Data Grids & Phantom Records
Posted: Tue Sep 11, 2012 1:22 pm
by sturgis
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)
Re: Arrays, Data Grids & Phantom Records
Posted: Tue Sep 11, 2012 1:33 pm
by donryan
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!
- Don