Delete Last elements in array
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Delete Last elements in array
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
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 couldYou could sort alphabetically perhaps. But you could not refer to the most recently inserted elements. Unless someone has a better idea.
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
Re: Delete Last elements in array
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
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
they are. the key is milliseconds so its numeric and ordered. I tried sparkouts method and it worked.
Re: Delete Last elements in array
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.
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
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
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
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]
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
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Delete Last elements in array
It's not so much language as array type. Associative arrays are by no means specific to LiveCode, supported in many languages: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.
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Delete Last elements in array
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.