What is the columnize() method in Euphoria?

Overview

The columnize() method in Euphoria is inbuilt and part of the sequence.e module. It can be used to create a table-like column structure from a sequence variable with substrings.

Each corresponding value from all sub-strings in a sequence are grouped into one single sequence. For example, the columnized value of seq1 = {{1,2,3},{4,5,6},{7,8,9}} will be {{1,4,7},{2,5,8},{3,6,9}

Syntax

public function columnize(source, cols, defval)

Parameters

  • source: This is a sequence variable, whose value will be columnized. It is usually a sequence value with other sub-sequences inside it.

  • cols: This specifies the column we want to return. It can be a range indicated by a set of numbers. For example, if we want the first and second columns from the source returned, the parameter cols should be: {1,2}. But if only the first column is to be returned, it should be 1.

  • defval: This is the value that will be used when a value is not provided for a column in the source. It is of the object data type and by default 0 which returns all columns.

Return value

This method returns a converted form of the sequence source whose set of sub sequences are converted into columns.

Code

In the code snippet below, the subsequence of a sequence are converted into columns using the columnize() method. A default value of 10 is used for the column with no value. The target column is the third (3) column.

include std/sequence.e
sequence source = {{1,2},{3,4,6},{7,8,9}}
object cols = 3
object defval = 0
sequence result
result = columnize(source, cols ,defval)
print(1,result)

Explanation

  • Line 1 : We include the sequence.e module which has the columnize method.
  • Lines 3 to 5: We declare and assign values to some variables.

  • Lines 6 and 7: We declare the result variable and store the outcome of the columnize() method in the result, respectively.

  • Line 8: We print the content of the result variable to screen.

Free Resources