Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove several items in a list simultaneously

Tags:

netlogo

How can I simultaneously remove several items in a list ? 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]
  • the last 9 items in the list :

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

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

Thank you very much for your help.

like image 712
Nell Avatar asked Feb 16 '14 03:02

Nell


1 Answers

You can use sublist to keep what you want from the list.

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

You can generalize this into a function:

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

which you can then use like: remove-last-n list1 4

Combining this with the unfortunately named 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 lets you 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]
like image 192
Bryan Head Avatar answered Oct 09 '22 16:10

Bryan Head