Page 1 of 1
Delete Last elements in array
Posted: Sat Mar 07, 2015 2:20 pm
by Da_Elf
this might seem simple but how do i go about with minimum code to delete lets say the last 10 entries of an array
Re: Delete Last elements in array
Posted: Sat Mar 07, 2015 4:10 pm
by SparkOut
First, define "the last 10 entries" of an array.
Livecode arrays are hash tables and not necessarily numerically indexed, and even if they are, the elements will not be sorted if you retrieve the keys.
So, assuming the array is numerically indexed you could
Code: Select all
put the keys of tArray into tKeys
sort tKeys numeric ascending
put line -10 to -1 of tKeys into tKeys
repeat for each line tDeleteKey in tKeys
delete variable tArray[(tDeleteKey)]
end repeat
You could sort alphabetically perhaps. But you could not refer to the most recently inserted elements. Unless someone has a better idea.
Re: Delete Last elements in array
Posted: Sat Mar 07, 2015 7:22 pm
by phaworth
As mentioned, would need to know if the elements are numerically indexed or not. If they are and if there are no missing numbers in the sequence, try this:
put the extents of tArray into tExtents
repeat with x= item 2 of tExtents down to (item 2 of tExtents - 10)
delete variable tArray[x]
end repeat
Pete
Re: Delete Last elements in array
Posted: Sat Mar 07, 2015 9:57 pm
by Da_Elf
they are. the key is milliseconds so its numeric and ordered. I tried sparkouts method and it worked.
Re: Delete Last elements in array
Posted: Sat Mar 07, 2015 11:03 pm
by SparkOut
OK good, just to be clear though - the elements of your array are NOT ordered. Even though they have numeric keys, they may keep any order in the associative array. When you retrieve the keys and put into a variable, the list in the variable can be sorted. Then you have an ordered list that you can use to manipulate the array elements by referring to them using lines of the sorted variable, but the elements themselves will never be sorted in the array.
Sorry to bang on about that but it is crucial to understanding how arrays in Livecode differ from (or resemble) any other language.
Re: Delete Last elements in array
Posted: Wed Mar 11, 2015 7:18 pm
by Da_Elf
so wait a sec. when the program is storing items into an array arnt they sequentially added? the first item is the first item and the last entered is the last?
Re: Delete Last elements in array
Posted: Wed Mar 11, 2015 9:21 pm
by SparkOut
No, correct, the order in which array elements may be retrieved is arbitrary.
You can only retrieve the nth element if you know what the key for n is, by obtaining the keys and sorting them.
Try stepping through the code and look at the variables list in the debugger for the tArr array when it is created
Code: Select all
breakpoint
put "1,2,3,4,5,6,7,8,9,10" into tArr
split tArr by comma
put the keys of tArr
If your keys are numerically sequenced then tArr[3] will be the third index, quite obviously. But tArr[3] may be stored internally between tArr[7] and tArr[2]
Re: Delete Last elements in array
Posted: Wed Mar 11, 2015 11:31 pm
by FourthWorld
SparkOut wrote:Sorry to bang on about that but it is crucial to understanding how arrays in Livecode differ from (or resemble) any other language.
It's not so much language as array type. Associative arrays are by no means specific to LiveCode, supported in many languages:
http://en.wikipedia.org/wiki/Associative_array
More flexible than simpler numerically-indexed arrays, the ability to use any alpha-numeric string as a key requires an underlying hash table, which provides very good speed at the cost of not also providing additional overhead to maintain an internal understanding of insertion order.
We may see numerically-indexed array at some point, but in the meantime most of the benefit of using them can be had in LiveCode right now with one-liner to sort the keys.
Re: Delete Last elements in array
Posted: Wed Mar 11, 2015 11:40 pm
by SparkOut
I did include (or resemble) in the quoted post, for exactly this purpose, ie to focus on the array type being flexible but not ordered without sorting the keys. Failing to understand this would trip up the op trying to delete the "last" n elements.