# Euphoria - Sequences

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

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 is 7.2.

Suppose x contains {11,22,33}, Now if you ask for x you get {11,22,33} and if you ask for x, 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
value of x = 2
value of x = 3
value of x = 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 = A
value of x = B
value of x = C
value of x = 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 = Hello
value of x = World
value of x = Euphoria
value of x =
value of x = 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,employee} )
```

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
value of x = -2
value of x = -3
value of x = -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} )
printf(1, "Script Name: %s\n", {x} )
printf(1, "First Argument: %s\n", {x})
printf(1, "Second Argument: %s\n", {x})
```

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
```