What is the extract() method in Euphoria?

Overview

The indexed nature of sequence variables makes it easy to access elements. This is because with these indexes, we can specifically pick out single or multiple elements in the sequence.

The extract() method uses the index of sequences to pick out elements from them. This method will accept a sequence with the indexes of elements that would be extracted from another sequence and returned.

For example, if we have a sequence A = "This is sports" and the sequence of indexes whose elements we wish to extract as B = {1,2,10,11}. The output will be a sequence C = {Thpo}.

Syntax

extract(source,index_list)

Parameters

  • source: This is the sequence from which values will be extracted.

  • index_list: This sequence contains a list of indexes that can be found in source whose values will be extracted.

Return value

This method returns a sequence value whose length is the same as the length of index_list.

Note: We can extract a particular item from the source multiple times by repeating its index in index_list. The returned sequence has items arranged in the same manner as the element’s index in index_list.

Example

include std/sequence.e
sequence source1, source2, result1, result2
source1 = {5,6,7,8,9,10}
source2 = "This is sports"
result1 = extract(source1 ,{4,5,3})
result2 = extract(source2,{1,2,10,11})
puts(1, "This is extracted from source1: ")
print(1,result1)
printf(1, "\nThis is extracted from source2: %s",{result2})

Explanation

  • Line 2: We include a module necessary for using the extract() method.
  • Line 4: We declare variables to be used later in our program.
  • Lines 5 and 6: We assign values to our earlier declared variables.
  • Lines 8 and 9: We save the output from the extract() to the result1 and result2 variables.
  • Lines 11–13: We print the output to the console.

Free Resources