What is the mid() method in Euphoria?

Overview

The mid() method is used to slice a sequence source from a start_point to a given length.

Syntax

mid(source,start_point,length)

Parameters

Let’s look at the different parameters used by this function below:

  • source: This is the sequence which will be spliced at the indicated point and to the indicated length.

  • start_point: This is an atom value which indicates an index in the source where the splicing will begin.

  • length: This the length which we want the sliced value to have, that is, how much we want the source to be spliced. It is an atom value and should be less that or equal to the length of the source variable.

Return value

The mid() method returns a sequence with elements less than or equal to the length parameter.

Example

--include the sequence module
include std/sequence.e
--declare some variables
sequence source1,source2
sequence outcome1, outcome2
--assign values to declared variables
source1 = {1,2,3,4,5,6,7}
source2 = "This is a good day"
--use the mid() method
outcome1 = mid(source1,2,4)
outcome2 = mid(source2,1,8)
print(1, {outcome1})
printf(1,"\n%s", {outcome2})

Explanation

  • Line 2: We include the sequence.e module.
  • Line 5 and 6: We declare some variables.
  • Lines 9 and 10: We assign values to the declared variables.
  • Lines 14 and 15: We assign the output of using mid() to the output1 and output2 variables.
  • Lines 17 and 18: We display the outcome.

Free Resources