Removing an element from a list
Maomao30-Jun-2018/9:33:41
Sorry, stupid question (beginner here): I can't find the answer: what is the easiest way to remove an element from a list?

mylist: ["1" "22" "333" "4444" "55555"]

This seems to work but... a little complicated?

remove skip mylist ((index? find mylist "333") - 1)
rb3330-Jun-2018/18:45:09
Hello,

You can use: remove at mylist index? find mylist "333"

You can create then a function:

remove-value: func [ blk [ block! ] value [ any-type! ] ] [
   not error? try [ remove at blk index? find blk value ]
]

remove-value mylist "33"
== false
remove-value mylist "333"
== true
? mylist
MYLIST is a block of value: ["1" "22" "4444" "55555"]


Good luck with Rebol. It is a very practical language once we have mastered the notion of block! string! and object!
DideC2-Jul-2018/8:28:45
Not bad, but why use index? :
mylist: ["1" "22" "333" "4444" "55555"]
attempt [remove find mylist "333"]

'find return the block at the position of the value found, so you just need to 'remove there
'attempt is here to catch the error if the 'find don't match.

Enjoy Rebol
DideC2-Jul-2018/14:57:56
If you don't want to use 'attempt, the shortest one is:
if p: find mylist "333" [remove p]
Maomao5-Jul-2018/8:44:19
Thanks!

Login required to Post.


Powered by RebelBB and REBOL 2.7.8.4.2