Historical FAQ, Revision 8

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.

Is there a tool to help find bugs or perform static analysis?

Answer me.

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

See Distributing a Program from the Euphoria Reference Manual.

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

Answer me.

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

See these Performance Tips.

Core Language

How do you set a global variable in a function?

Answer me.

What are the rules for local and global variables in Euphoria?

Answer me.

How do I share global variables across modules?

Answer me.

What are the "best practices" for using import in a module?

Answer me.

How can I pass optional or keyword parameters from one function to another?

Answer me.

How do I write a function with output parameters (call by reference)?

Answer me.

How do you make a higher order function in Euphoria?

Answer me.

How do I copy an object in Euphoria?

Answer me.

How can I find the methods or attributes of an object?

Answer me.

How can my code discover the name of an object?

Answer me.

Is there an equivalent of C's "?:" ternary operator?

Answer me.

Is it possible to write obfuscated one-liners in Euphoria?

Answer me.

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.

Why does -22 / 10 return -3?

Answer me.

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.

How do I modify a string in place?

Answer me.

How do I use strings to call functions/methods?

Answer me.

Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?

Answer me.

Is there a scanf() or sscanf() equivalent?

Answer me.

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.

When does a local variable become accessible?

Answer me.

How are arguments passed?

Answer me.

What does '#' prepended to an argument mean?

Answer me.

Does the value of a constant ever change?

Answer me.

Does Euphoria support keyword arguments?

Answer me.

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 are the non-alphanumerical symbols in Euphoria code?

Answer me.

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 do random number seeds work?

Answer me.

How can I count the frequency of words in a file?

Answer me.

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.

Does Euphoria have function pointers?

Answer me.

Does Euphoria have exception handling?

Answer me.

How can I move or copy a file?

Answer me.

Sequences

What's a negative index?

Answer me.

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) 

How do I create a multidimensional list?

Answer me.

How do I apply a method to a sequence of objects?

Answer me.

Dictionaries

How can I get a dictionary to display its keys in a consistent order?

Answer me.

I want to do a complicated sort: can you do a Schwartzian Transform in Euphoria?

Answer me.

How can I sort one list by values from another list?

Answer me.

Input and Output

How can I process a file and update its contents?

Answer me.

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.

How do I get a recursive list of all the files under a directory and its subdirectories?

Answer me.

How can I control output buffering?

Answer me.

Extensions

How can I use Euphoria interactively?

Answer me.

Is there a debugger for Euphoria?

Answer me.

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 Tcl/Tk interface in Euphoria?

Answer me.

Can I use gtk+ interfaces in Euphoria?

Yes - EuGTK

Can I use xforms interfaces in Euphoria?

Answer me.

How can I do date arithmetic?

Answer me.

Can I integrate Euphoria and Python?

Answer me.

XML parsers

Answer me.

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.

What is a class?

Answer me.

What is a method?

Answer me.

What is self?

Answer me.

How do I check if an object is an instance of a given class or of a subclass of it?

Answer me.

What is delegation?

Answer me.

How do I call a method defined in a base class from a derived class that overrides it?

Answer me.

How can I organize my code to make it easier to change the base class?

Answer me.

How do I create static class data and static class methods?

Answer me.

How can I overload constructors (or methods) in Euphoria?

Answer me.

I try to use {{{__spam}}} and I get an error about {{{_SomeClassName__spam}}}.

Answer me.

My class defines {{{__del__}}} but it is not called when I delete the object.

Answer me.

How do I get a list of all instances of a given class?

Answer me.

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.

General Euphoria FAQ

Euphoria Libraries FAQ

Extending/Embedding Euphoria FAQ

GUI Programming FAQ

Search



Quick Links

User menu

Not signed in.

Misc Menu