The retain_all()
method is used to manipulate Euphoria sequences. This method will remove all the elements of a haystack sequence except for those present in the needle sequence.
retain_all(needles,haystack)
needles
: This is the set of elements to be retained if found in a haystack
. It is a data type object and it is a required parameter.
haystack
: This is the sequence containing elements that will be removed if not found as part of needles
.
This method returns a new sequence that contains only those elements from haystack
that are also in needles
. An empty sequence will be returned if none of the elements of needles
are found in haystack
.
Let’s look at the code below:
--add a required moduleinclude std/sequence.e--declare variablesobject needle1, needle2, needle3sequence haystack1, haystack2, haystack3sequence output1, output2, output3--initialize variableshaystack1 = {1,2,4,5,6,7}haystack2 = {8,9,10,11,12,17}haystack3 = {20,30,40,50}needle1 = {3,4}needle2 = {8,9,10,17}needle3 = {80,90}--call the retain_all() method and save its outputoutput1 = retain_all(needle1, haystack1)output2 = retain_all(needle2, haystack2)output3 = retain_all(needle3, haystack3)--print the output to displayprint(1,output1)puts(1,"\n")print(1,output2)puts(1,"\n")print(1,output3)puts(1,"\n")
Line 2: We add a required module.
Lines 5 to 7: We declare some variables.
Lines 10 to 16: We assign values to the declared variables.
Lines 18 to 20: We call the retain_all()
method and store the resulting sequences in output variables.
Line 22 to 27: We print all the output variables.
The output in line 27 is an empty sequence ({}
) because none of the needles in variable needle3
are found in the haystack parameter, haystack3
. Therefore, there is nothing to retain.