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 thesourcereturned, the parametercolsshould be:{1,2}. But if only the first column is to be returned, it should be1. -
defval: This is the value that will be used when a value is not provided for a column in thesource. It is of the object data type and by default0which 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.esequence source = {{1,2},{3,4,6},{7,8,9}}object cols = 3object defval = 0sequence resultresult = columnize(source, cols ,defval)print(1,result)
Explanation
- Line 1 : We include the
sequence.emodule which has thecolumnizemethod.
-
Lines 3 to 5: We declare and assign values to some variables.
-
Lines 6 and 7: We declare the
resultvariable and store the outcome of thecolumnize()method in theresult, respectively. -
Line 8: We print the content of the
resultvariable to screen.