### Historical FAQ, Revision 20

Here are most of the questions that new comers to the language tend to ask at some time.

## Programming Euphoria FAQ

### General Questions

#### Is there a source code level debugger with breakpoints, single-stepping, etc.?

Yes. See Debugging and Profiling in the Manual.

#### How can I create a stand-alone binary from a Euphoria script?

See Distributing a Program in the Manual.

#### Are there coding standards or a style guide for Euphoria programs?

See Euphoria CodingConvention.

#### My program is too slow. How do I speed it up?

See Performance Tips in the Manual.

### Core Language

See Scope.

#### How do I share global variables across modules?

Use the "global" variable definition.

### Numbers, strings, math

#### How do I generate random numbers in Euphoria?

See Random Numbers in the Manual.

#### How do I specify hexadecimal and octal integers?

For hexadecimal, use the '#' symbol (#FE, #A000, etc.). It is not possible to input octal values, but it is possible to output them. See printf().

#### How do I convert a string to a number?

Use the value() function.

#### How do I convert a number to a string?

Use the sprintf() function.

### Variables, constants, and arguments

See Scope.

#### How are arguments passed?

All arguments are passed by value.

#### Does the value of a constant ever change?

No. The value of a constant is fixed when it's defined for the life of the program.

### Syntax

#### How can I code post-test loops?

See loop until in the Manual.

An example of the loop..until construct:

i = 0
loop do
printf(1, "i = %d\n", {i} )
i += 1
until i > 4

#### What does \$ mean?

The symbol '\$' in Euphoria version 2.5 and later means "length(of_sequence)."

sequence s, i
s = "My Sequence"
i = s[\$] -- i = "e"
i = s[4..\$] -- i = "Sequence"
i = s[\$-5] -- i = "q"

### Miscellaneous

#### How can I sort strings in alphabetical order?

See the sort code snippet.

#### How can I expand tabs to spaces?

See the replace element(s) code snippet.

### Sequences

#### How do I iterate over a sequence in reverse order?

Use a for..loop, like so

sequence s
for t=length(s) to 1 by -1 do
-- do something with s[t]
end for

#### How do you remove duplicates from a sequence?

Here's one way

global function rem_dupes( sequence group )
sequence new
new = {} -- initialize new to empty sequence
for t=1 to length(group) do -- for each element in group
if find(group[t],new) = 0 then -- add each element once
new = append(new,group[t])
end if
end for
return new
end function

Using Eu4.0:

include std/sequence.e

sequence a = {1,5,2,7,3,67,3,2,4,6,1}
? remove_dups(a,RD_INPLACE) -- returns {1,5,2,7,3,67,4,6}
? remove_dups(a,RD_SORT)-- returns {1,2,3,4,5,6,7,67}

sequence b = {"Jim","Joe","Sue","Sal","Jim"}
b = remove_dups(b,RD_SORT)
for i =  1 to length(b) do
printf(1,"%d %s\n",{i,b[i]})
end for
-- prints:

1 Jim
2 Joe
3 Sal
4 Sue

#### How do you make an array in Euphoria?

Sequences are arrays. You create them by first declaring a variable as sequence, then assigning elements to the sequence. For example

sequence s
s = { 1, 2, 3 } -- s is a 3-element sequence
s = repeat(0,20) -- s is a 20-element sequence of zeroes
s = repeat(repeat(1,10),20) -- s is a 20-element sequence of 10-element sequences

#### How do you refer to individual elements in an array?

You get the value from a sequence with bracketed values. For instance,

?s[5][3] -- prints the third element of the fifth sequence (a multi-dimensional sequence)

### Input and Output

#### Should I feel uneasy if I don't close a file?

You should always close any files you no longer need open. However, Euphoria will automatically close any still-open files when your program terminates.

### Extensions

#### How can I use a library written in C from Euphoria?

You can access shared libraries from a Euphoria program. You can use code from a shared library uing open_dll(libname). This returns an atom which you'll have to reuse to access individual variables or functions.

You can access a function in a previously opened shared library by calling define_c_function(entry_point,func_name,type_arglist,return_type). This return an id that you can use with call_func().

define_c_proc() and define_c_var() work in the same way, but require less arguments for obvious reasons. You can define a function as a procedure if you'll never care about the returned value. You can access executable machine code using the above. Use "" as an entry point, and the address as name.

You can call(address) so that code at address gets executed. The code must be a routine that returns using the near ret instructions (opcode #C3). All used registers must be restored upon return. You'll have to set up the memory area which you'll call() into by coding some machine code into a sequence, allocate() a memory block of the right size and poke()ing the sequence first.

You must be aware that the code will run in 32 bit protected mode at privilege 3, and that call() requires two task switches, penalizing performance unless the asm code has a lot of work to perform. (1)

#### Can I use gtk+ interfaces in Euphoria?

Yes - for Linux only: EuGTK

#### Is there a Euphoria library for wxWidgets?

Yes. Matthew Lewis maintains his wxEuphoria library here.

### Other Features

#### How can I count the number of lines in a file?

Here's one way

integer sum, fn
object line
sum = 0
fn = open("myfile","r")
line = gets(fn)
while not atom(line) do
sum += 1
line = gets(fn)
end while
printf(1,"Lines in file: %d\n",{sum})

If you prefer Eu4.0, this works:

include std/io.e

sequence fname = "GtkEngine.e"

printf(1,"There are %d lines in file %s\n",{length(lines),fname})

#### How can I sum the elements in an array?

Use a for loop

integer sum
sequence a
a = {1,2,3,4,5,6,7,8}
sum = 0
for t=1 to length(a) do
sum += a[t]
end for

Or, with Eu4.0:

include std/math.e
sequence a = {1,2,3,4,5,6,7,8}
? sum(a)

#### Is there a CGI module for Euphoria?

There is no standard CGI module that gets distributed with the Euphoria base package. However, there is code available for doing CGI with Euphoria (obviously, because this entire website is being served by a Euphoria CGI program). You can also get specific information for running Euphoria as CGI here and at the official Euphoria web site.

### Object-Orientedness

#### Does Euphoria support object oriented programming?

Vanilla Euphoria does not "support" OO programming, though there are OO libraries in the archive, and Matthew Lewis has created a Euphoria interpreter with built-in object-oriented features, which can be downloaded here.

## Converting from version 3 to version 4

#### Why does my program take longer to load?

Symbol scope and resolution has changed in v4. Most v3 code will work correctly with respect to include files and global symbols, but if you use a global symbol without including the file in which it was defined (or another file that includes the correct file) then euphoria will take a little longer to resolve that reference. It is advised that if you use any variable or routine from some file or library, that each using file include the file or library that defines the symbols being used.