a sequence is represented by a list of objects in brace brackets { }, separated by commas. a sequence can contain both atoms and other sequences. for example −
{2, 3, 5, 7, 11, 13, 17, 19} {1, 2, {3, 3, 3}, 4, {5, {6}}} {{"zara", "ayan"}, 52389, 97.25} {} -- the 0-element sequence
a single element of a sequence may be selected by giving the element number in square brackets. element numbers start at 1.
for example, if x contains {5, 7.2, 9, 0.5, 13} then x[2] is 7.2.
suppose x[2] contains {11,22,33}, now if you ask for x[2] you get {11,22,33} and if you ask for x[2][3], you get the atom 33.
example
#!/home/euphoria-4.0b2/bin/eui sequence x x = {1, 2, 3, 4} for a = 1 to length(x) do printf(1, "value of x[%d] = %d\n", {a, x[a]}) end for
here, length() is the built-in function which returns length of the sequence. the above example produces the following result −
value of x[1] = 1 value of x[2] = 2 value of x[3] = 3 value of x[4] = 4
character string
a character string is just a sequence of characters. it may be entered in one of the two ways −
(a) using double quotes −
"abcdefg"
(b) using raw string notation −
-- using back-quotes `abcdefg` or -- using three double-quotes """abcdefg"""
you can try the following example to understand the concept −
#!/home/euphoria-4.0b2/bin/eui sequence x x = "abcd" for a = 1 to length(x) do printf(1, "value of x[%d] = %s\n", {a, x[a]}) end for
this produces the following result −
value of x[1] = a value of x[2] = b value of x[3] = c value of x[4] = d
string arrays
an array of strings can be implemented using sequences as follows −
#!/home/euphoria-4.0b2/bin/eui sequence x = {"hello", "world", "euphoria", "", "last one"} for a = 1 to length(x) do printf(1, "value of x[%d] = %s\n", {a, x[a]}) end for
this produces the following result −
value of x[1] = hello value of x[2] = world value of x[3] = euphoria value of x[4] = value of x[5] = last one
euphoria structures
a structure can be implemented using sequences as follows −
#!/home/euphoria-4.0b2/bin/eui sequence employee = { {"john","smith"}, 45000, 27, 185.5 } printf(1, "first name = %s, last name = %s\n", {employee[1][1],employee[1][2]} )
this produces the following result −
first name = john, last name = smith
there are various operations which can be performed directly on sequences. let us see them in detail −
urinary operation
when applied to a sequence, a unary operator is actually applied to each element in the sequence to yield a sequence of results of the same length.
#!/home/euphoria-4.0b2/bin/eui sequence x x = -{1, 2, 3, 4} for a = 1 to length(x) do printf(1, "value of x[%d] = %d\n", {a, x[a]}) end for
this produces the following result −
value of x[1] = -1 value of x[2] = -2 value of x[3] = -3 value of x[4] = -4
arithmetic operations
almost all arithmetic operations can be performed on sequences as follows −
#!/home/euphoria-4.0b2/bin/eui sequence x, y, a, b, c x = {1, 2, 3} y = {10, 20, 30} a = x + y puts(1, "value of a = {") for i = 1 to length(a) do printf(1, "%d,", a[i]) end for puts(1, "}\n") b = x - y puts(1, "value of b = {") for i = 1 to length(a) do printf(1, "%d,", b[i]) end for puts(1, "}\n") c = x * 3 puts(1, "value of c = {") for i = 1 to length(c) do printf(1, "%d,", c[i]) end for puts(1, "}\n")
this produces the following result −
value of a = {11,22,33,} value of b = {-9,-18,-27,} value of c = {3,6,9,}
command line options
a user can pass command line options to a euphoria script and it can be accessed as a sequence using command_line() function as follows −
#!/home/euphoria-4.0b2/bin/eui sequence x x = command_line() printf(1, "interpeter name: %s\n", {x[1]} ) printf(1, "script name: %s\n", {x[2]} ) printf(1, "first argument: %s\n", {x[3]}) printf(1, "second argument: %s\n", {x[4]})
here printf() is euphoria's built-in function. now if you run this script as follows −
$eui test.ex "one" "two"
this produces the following result −
interpeter name: /home/euphoria-4.0b2/bin/eui script name: test.ex first argument: one second argument: two