Variables

There are two types of data in the Euphoria programming language: atoms and sequences. Atoms hold single values and sequences hold a series of values. In order to use variables, we must declare them at the very beginning of our program. Here's what a variable declaration looks like:

atom x
sequence y

This tells the computer that the variable 'x' will hold data of type atom, and y will hold data of type sequence. By declaring these variables as atom or sequence, we let the interpreter know what to expect.

Storing values in variables is called assignment. The command

x = 5

is an assignment and means "assign (or store) the value of 5 to the variable x." Thank goodness we don't have to type that every time we want to store an item of data!

Atoms are single values (numbers). x is an atom. It stores the value 5.

Look at this assignment:

s = { 1, 2, 3 }

A sequence can hold atoms and other sequences. In the example above, we are assigning the sequence { 1, 2, 3 }, which is a series of three atoms, to the variable named s. Each item of data in a sequence is called an element. So, the variable s has three elements after the above assignment. The squiggly brackets are required when defining a sequence.

Here is a more complicated sequence assignment:

s = { 1, 2, { 1, 2, 3 } }

Notice that the first and second elements of s are atoms, but the third element is a sequence, which is itself a sequence of three atoms. Here are some more sequence assignments for you to consider:

s = { 1 }

In this one, we've assigned the sequence { 1 } to s. Yes! A sequence can have only one element. It can also have none, like this:

s = {}

That just means that s is an empty sequence at the moment.

s = { { 1 } , { 2 } , { 3 } }

s is now a sequence of three elements, each of which is a sequence with only one element each!

s = { { { 1 , { 2 , 3 } , 2 , { 1 , 2 , 3 } } , 2 , 3 } }

Have fun with that one!

When you want to reference an element in a sequence, you use the square brackets, like this:

s = { 1, 2, 3 }
?s[1]

1

The print command tells the PC to print the first value of the sequence s, which in this case is a '1'.

In this next series, s is now a sequence of 4 elements, with the third element being another sequence.

s = { 1, 2, { 1, 2, 3 } , 3 }

See if you can determine what the output of the following will be.

?s[2]
?s[3]
?s[3][2]

Here's the output:

2
{ 1, 2, 3 }
2

Look at that last command. Notice that we referenced the second element of the third element of s by using another set of brackets! Simple, huh?

One final note about sequences: you can use quotes to indicate a sequence of letters, also referred to as a string. For example,

s = "Hello 123"

When we use the ? command, we get the following output:

{72,101,108,108,111,32,49,50,51}

So, you might can guess that the following two assignments are equal:

s = "Hello 123"
s = {72,101,108,108,111,32,49,50,51}

Euphoria always sees it as the second assignment. If you want to see it in human readable/understandable format, you need to use puts(). Remember, puts() means "output sequence."

s = {72,101,108,108,111,32,49,50,51}
puts(1, s )

Hello 123

This distinction will come into play later, but for now it's good to know that quotes are used for text strings while the brackets are used for numeric data. That means the following are NOT equal assignments:

s = { Hello }
s = "Hello"

The first assigns a sequence with the element Hello to s. If Hello is not a previously defined variable, you'll get an error. The second assigns the string "Hello" to s, which is a valid assignment.

s = { H , 2 , Program , X }

This assignment is only valid if 'H,' 'Program,' and 'X' are previously defined variables. If not, you'll get an error.

These are all valid sequence assignments:

s = { 1, 0, -4, 13 }
s = { "1, 2, and 3 are nice." }
s = { " { 1, 2, 3 } , { 2, 4, 6 } " }

Euphoria has two more data types, but these are actually special cases of atoms and/or sequences. The integer variable is an atom that only holds integer values.

x = 1.3
y = 1

In this example assignment, x and y are both atoms, but only y is an integer. As you can see, y is really just a special case of atom. This will be important later.

The object is a special variable that can hold either atoms or sequences. If, for instance, you tell the PC that x will be an atom, then try to assign a sequence to it, you'll get an error message, and vice versa. Sequence variables can't hold atoms. Generally, you'll know in advance what a variable will hold and you will specify that with the following variable declarations:

atom x
sequence y

That declaration tells the PC that the variable 'x' is going to hold atom values only, and the variable 'y' is going to hold sequence values only. If you try to assign a sequence to x, you'll get an error message; the same if you try to assign an atom to y.

If you don't know in advance what a variable will hold, or you know that the variable will need to hold either atoms or sequences, you'll use the object declaration.

object x

Now x can hold either atoms or sequences. This series of commands is now perfectly valid:

object x
x = 1 --< first an integer
x = "a string" --< now a string (sequence)
x = 2.74 --< now an atom
x = {"one string","two strings"} --< now a sequence of two strings

One more thing about atoms and integers. They each have a limited value range. Atoms can have any integer or double-precision floating point value. They can range from approximately -1e300 (negative one times 10 to the power 300) to +1e300 with 15 decimal digits of accuracy. Integer variables can hold integer values from -1073741824 to +1073741823 inclusive.