Previous Up Next

6.3.8  Removing elements from a list

Elements can be removed from a list by using either the suppress command (which removes a single element) or the remove command (which removes all elements meeting a given conditions).

Examples

suppress([3,4,2],1)
     

3,2
          
remove(x->(x>=2),[0,1,2,3,1,5])
     

0,1,1
          

You can use remove to remove characters from a string. For example, to remove all instances of a given letter from a string, enter the following code in a program editor level (see Section 25.1.2 for writing functions):

f(s,c):={ local ordc,l,r,x,k; ordc:=ord(c); l:=length(s)-1; purge(x,k); r:=remove(x->(ord(x)==ordc),seq(s[k],k,0,l)); return char(ord(r)); }

Here s is the input string an c is the letter to be removed. Now, for example:

f("abracadabra","a")
     
“brcdbr”           

Previous Up Next