How to delete multiple items in a list at once

How can I delete multiple items in a list at the same time? I have a list:

let list1 map [ -1 * ? ] reverse (n-values ( ( max-pxcor -  round (max-pxcor / 3) ) + 1 ) [?]) 
print list1
[-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0]

For example, I would like to remove: - the last 4 items in the list:

[-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4]
  • last 9 items in list:

    [-17 -16 -15 -14 -13 -12 -11 -10 -9]
    
  • last 13 items in list:

    [-17 -16 -15 -14 -13]
    

Many thanks for your help.

+3
source share
1 answer

You can use sublistto save what you want from the list.

For example, delete the last 4 items in the list: sublist list1 0 (length list1 - 4)

You can generalize this to a function:

to-report remove-last-n [ lst n ]
  report sublist lst 0 (length lst - n)
end

which you can use as follows: remove-last-n list1 4

Combining this, unfortunately, with the name sentence, you can remove arbitrary sublists:

to-report remove-sublist [ lst position1 position2 ]
  report (sentence (sublist lst 0 position1) (sublist lst position2 length lst))
end

which allows you to do

observer> show remove-sublist [-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0] 2 13
observer: [-17 -16 -4 -3 -2 -1 0]
+2
source

All Articles