Delete Last elements in array

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

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Delete Last elements in array

Post by Da_Elf » Sat Mar 07, 2015 2:20 pm

this might seem simple but how do i go about with minimum code to delete lets say the last 10 entries of an array

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete Last elements in array

Post by SparkOut » Sat Mar 07, 2015 4:10 pm

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.

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Delete Last elements in array

Post by phaworth » Sat Mar 07, 2015 7:22 pm

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

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Delete Last elements in array

Post by Da_Elf » Sat Mar 07, 2015 9:57 pm

they are. the key is milliseconds so its numeric and ordered. I tried sparkouts method and it worked.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete Last elements in array

Post by SparkOut » Sat Mar 07, 2015 11:03 pm

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.

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Delete Last elements in array

Post by Da_Elf » Wed Mar 11, 2015 7:18 pm

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?

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete Last elements in array

Post by SparkOut » Wed Mar 11, 2015 9:21 pm

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]

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Delete Last elements in array

Post by FourthWorld » Wed Mar 11, 2015 11:31 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Delete Last elements in array

Post by SparkOut » Wed Mar 11, 2015 11:40 pm

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.

Post Reply