# Frequently Asked Questions

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 the Debugging and Profiling article at RDS' home page.

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

See Distributing a Program from the Euphoria Reference Manual.

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

See these Performance Tips.

### Numbers, strings, math

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

Euphoria uses the rand() function to generate random integers. See rand on this page.

#### 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 on this page.

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

Use Euphoria's value() function. See value on this page.

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

Use Euphoria's sprintf() function. See sprintf on this page.

### Variables, constants, and arguments

#### What is the scope of a local variable? a constant?

See section 2.4.2 Scope in the Euphoria Reference Manual.

### Syntax

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

If you need loops that test the condition at the end of the block, rather than at the start, use a hard while loop with an exit statement in either euphoria v3 or v4.

```i = 0
while 1 do
printf(1, "i = %d\n", {i} )
i += 1
if i > 4 then
exit
end if
end while
```

Alternatively, in v4, you can use the loop..until construct:

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

#### What does \$ mean?

The symbol '\$' has meaning in Euphoria version 2.5 and later. It 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
```

#### 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 interface Euphoria programs with other programs?

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. Irv Mullins has GTK code here.

#### 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})
```

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

#### 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.