Wiki Diff FAQ, revision #23 to tip

= Frequently Asked Questions =
Here are most of the questions that new comers to the language tend to ask at some time.
<<toc level=4>>

== Programming Euphoria FAQ
=== General Questions

==== Is there a source code level debugger with breakpoints, single-stepping, etc.?
Yes. See [[http://openeuphoria.org/docs/debug.html#_512_debuggingandprofiling|Debugging and Profiling]] in the Manual.

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

-TEST (all)
Parses the code only and issues any warnings or errors to STDOUT. On error the exit code will be 1, otherwise 0. If an error was found, the normal "Press Enter" prompt will not be presented when using the -TEST parameter which enables many editor/IDE programs to test the syntax of your Euphoria source in real time.

==== How can I create a stand-alone binary from a Euphoria script?
See [[http://openeuphoria.org/docs/distributing.html|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 [[http://openeuphoria.org/docs/perform.html#_639_performancetips|Performance Tips]] in the Manual.

=== Core Language

==== How do you set a global variable in a function?
Assign a value to it.

==== What are the rules for local and global variables in Euphoria?
See [[Scope]].

==== How do I share global variables across modules?
Use the "global" variable definition.

==== 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?
Euphoria 4 offers some functions to simplify this:
[[http://openeuphoria.org/docs/std_text.html#_3395_keyvalues|text:keyvalues]]

==== How do I write a function with output parameters (call by reference)?
Euphoria doesn't support call by reference. If you want your function to modify a variable, assign the return value of the function to that variable.

==== How do you make a higher order function in Euphoria?
<eucode>
procedure use(integer fi, integer a, integer b)
print(1,call_func(fi,{a,b}))
end procedure

function add(integer a, integer b)
return a + b
end function

use(routine_id("add"),23,45)
</eucode>

==== How do I copy an object in Euphoria?
a=b

==== How can I find the methods or attributes of an object?
Euphoria isn't object oriented.\\
However, several "object oriented" libraries have been created, see, for example, EuGTK, where all GTK object methods and attributes are accessible.

==== How can my code discover the name of an object?
Euphoria isn't an object oriented language.

==== Is there an equivalent of C's "?:" ternary operator?
see std/utils.e iif() function

==== Is it possible to write obfuscated one-liners in Euphoria?
Why do you ask?

=== Numbers, strings, math

==== How do I generate random numbers in Euphoria?
See [[http://openeuphoria.org/docs/std_rand.html#_3750_randomnumbers|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 [[http://openeuphoria.org/docs/std_get.html#_2245_value|value]]() function.\\
With Euphoria 4, use the [[http://openeuphoria.org/docs/std_convert.html#_2293_to_number|convert:to_number() function]]

==== How do I convert a number to a string?
Use the [[http://openeuphoria.org/docs/std_text.html#_3240_sprint|sprint]]() or[[http://openeuphoria.org/docs/std_text.html#_3233_sprintf|sprintf]]() function.\\
Euphoria 4 also has [[http://openeuphoria.org/docs/std_text.html#_3429_format| text:format()]] which offers a wide selection of formatting options.

==== How do I modify a string in place?
Just do it. \\
name = "Abe Lincoln"\\
name = "Abraham Lincoln" (now it's modified. Was that so hard?)

==== How do I use strings to call functions/methods?
See
[[http://openeuphoria.org/docs/dynamic.html#_639_callingeuphoriaroutinesbyid|routine_id()]] in the docs.

==== Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
See [[http://openeuphoria.org/docs/std_text.html#_3260_trim|trim]](), [[http://openeuphoria.org/docs/std_text.html#_3248_trim_head|trim_head]](), and [[http://openeuphoria.org/docs/std_text.html#_3254_trim_tail|trim_tail]]().

==== Is there a scanf() or sscanf() equivalent?
see value()

=== Variables, constants, and arguments

==== What is the scope of a local variable? a constant?
See [[Scope]].

==== When does a local variable become accessible?
Answer me.

==== How are arguments passed?
All arguments are passed by value.

==== What does '#' prepended to an argument mean?
A better question would be which programming language allows prepending a '#' to an argument.
If you answer Racket you get an A.

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

==== Does Euphoria support keyword arguments?
No. There are ways to simulate keyword arguments, however, using functions in the std library.\\
See [[http://openeuphoria.org/docs/std_text.html#_3395_keyvalues|keyvalues]]

=== Syntax

==== How can I code post-test loops?
See [[http://openeuphoria.org/docs/lang_loop.html#_150_loopuntilstatement|loop until]] in the Manual.

An example of the ##loop..until## construct:

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

==== What are the non-alphanumerical symbols in Euphoria code?

==== What does $ mean?
The symbol '$' in Euphoria version 2.5 and later means "length(of_sequence)."

<eucode>
sequence s, i
s = "My Sequence"
i = s[$] -- i = "e"
i = s[4..$] -- i = "Sequence"
i = s[$-5] -- i = "q"
</eucode>

=== 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 [[http://openeuphoria.org/docs/std_sort.html#_2881_sort|sort]]().

==== How can I expand tabs to spaces?
See [[http://openeuphoria.org/docs/std_search.html#_2352_find_replace|find_replace]]() and [[http://openeuphoria.org/docs/std_search.html#_2361_match_replace|match_replace]]().

==== Does Euphoria have function pointers?
see routine_id

==== Does Euphoria have exception handling?
No.

==== How can I move or copy a file?
See [[http://openeuphoria.org/docs/std_filesys.html#_1404_copy_file|copy_file]]() and [[http://openeuphoria.org/docs/std_filesys.html#_1416_move_file|move_file]]().

=== Sequences

==== What's a negative index?
Negative index is a mis-named feature of Python and some other languages. If the index is n, and n = -1,
then this means, to python, in English, "the last". In Euphoria, this would be x[length(x)], or in a more convenient shorthand x[$]. In Python, -2 would mean "next to last". In Euphoria x[length(x)-1] or x[$-1].

==== How do I iterate over a sequence in reverse order?
Use a for..loop, like so

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

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

See [[http://openeuphoria.org/docs/std_sequence.html#_2816_remove_dups|remove_dups]]().

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

<eucode>
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
</eucode>

==== How do you refer to individual elements in an array?
You get the value from a sequence with bracketed values. For instance,

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

==== 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?
The term "Schwartzian transform" is the perl name for a sorting algorithm which can be written in
almost any turing-complete programming language, including Euphoria. Euphoria is likely to be faster than perl.

==== 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?

See [[http://openeuphoria.org/docs/std_io.html#_1624_filereadingwriting|File Reading/Writing]].

One way to do this is:

# Use [[http://openeuphoria.org/docs/std_io.html#_1626_read_lines|read_lines]]() to read in the contents of a file.
# Manipulate the sequence of lines as desired
# Use [[http://openeuphoria.org/docs/std_io.html#_1641_write_lines|write_lines]]() to write the sequence back to a file.

==== 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?
flush.

=== Extensions

==== How can I use Euphoria interactively?
You don't.

==== 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 [[http://openeuphoria.org/docs/std_dll.html#_5345_open_dll|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 - [[https://sites.google.com/site/euphoriagtk/]] for Linux, Windows, and OS X.
EuGTK can load and run XML interface code built by Glade, so you can simply drag-and-drop to build your
program's user interface.

==== Can I use xforms interfaces in Euphoria?
Answer me.

==== How can I do date arithmetic?
See the [[http://openeuphoria.org/docs/std_datetime.html#_957_datetime|DateTime]] standard library. In particular, [[http://openeuphoria.org/docs/std_datetime.html#_1102_add|add\\() and [[http://openeuphoria.org/docs/std_datetime.html#_1109_subtract|subtract]]().

==== Can I integrate Euphoria and Python?
Answer me. Better yet, explain why you would want to do this.

==== XML parsers
There are several in the archives.

==== Is there a Euphoria library for wxWidgets?
Yes. See Matthew Lewis' [[http://wxeuphoria.sourceforge.net/|wxEuphoria library]].

=== Other Features

==== How can I count the number of lines in a file?
Here's one way:

<eucode>
include std/io.e

sequence fname = "GtkEngine.e"

object lines = read_lines(fname)
printf(1,"There are %d lines in file %s\n",{length(lines),fname})
</eucode>

==== How can I sum the elements in an array?
Here's one way:

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

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

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








= Frequently Asked Questions =
Here are most of the questions that new comers to the language tend to ask at some time.
<<toc level=4>>

== Programming Euphoria FAQ
=== General Questions

==== Is there a source code level debugger with breakpoints, single-stepping, etc.?
Yes. See [[http://openeuphoria.org/docs/debug.html#_512_debuggingandprofiling|Debugging and Profiling]] in the Manual.

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

-TEST (all)
Parses the code only and issues any warnings or errors to STDOUT. On error the exit code will be 1, otherwise 0. If an error was found, the normal "Press Enter" prompt will not be presented when using the -TEST parameter which enables many editor/IDE programs to test the syntax of your Euphoria source in real time.

==== How can I create a stand-alone binary from a Euphoria script?
See [[http://openeuphoria.org/docs/distributing.html|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 [[http://openeuphoria.org/docs/perform.html#_639_performancetips|Performance Tips]] in the Manual.

=== Core Language

==== How do you set a global variable in a function?
Assign a value to it.

==== What are the rules for local and global variables in Euphoria?
See [[Scope]].

==== How do I share global variables across modules?
Use the "global" variable definition.

==== 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)?
Euphoria doesn't support call by reference. If you want your function to modify a variable, assign the return value of the function to that variable.

==== How do you make a higher order function in Euphoria?
Answer me.

==== How do I copy an object in Euphoria?
a=b

==== How can I find the methods or attributes of an object?
Euphoria isn't object oriented.

==== How can my code discover the name of an object?
Euphoria isn't an object oriented language.

==== Is there an equivalent of C's "?:" ternary operator?
Answer me.

==== Is it possible to write obfuscated one-liners in Euphoria?
Why do you ask?

=== Numbers, strings, math

==== How do I generate random numbers in Euphoria?
See [[http://openeuphoria.org/docs/std_rand.html#_3750_randomnumbers|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 [[http://openeuphoria.org/docs/std_get.html#_2245_value|value]]() function.

==== How do I convert a number to a string?
Use the [[http://openeuphoria.org/docs/std_text.html#_3240_sprint|sprint]]() or[[http://openeuphoria.org/docs/std_text.html#_3233_sprintf|sprintf]]() function.

==== How do I modify a string in place?
Just do it.
name = "Abe Lincoln"
name = "Abraham Lincoln" -- now it's modified. Was that so hard?

==== How do I use strings to call functions/methods?
see routine_id in the docs.

==== Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
See [[http://openeuphoria.org/docs/std_text.html#_3260_trim|trim]](), [[http://openeuphoria.org/docs/std_text.html#_3248_trim_head|trim_head]](), and [[http://openeuphoria.org/docs/std_text.html#_3254_trim_tail|trim_tail]]().

==== Is there a scanf() or sscanf() equivalent?
see value

=== Variables, constants, and arguments

==== What is the scope of a local variable? a constant?
See [[Scope]].

==== When does a local variable become accessible?
Answer me.

==== How are arguments passed?
All arguments are passed by value.

==== What does '#' prepended to an argument mean?
A better question would be which programming language allows prepending a '#' to an argument.

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

==== Does Euphoria support keyword arguments?
No. There are ways to simulate keyword arguments, however, in the std library.

=== Syntax

==== How can I code post-test loops?
See [[http://openeuphoria.org/docs/lang_loop.html#_150_loopuntilstatement|loop until]] in the Manual.

An example of the ##loop..until## construct:

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

==== What are the non-alphanumerical symbols in Euphoria code?
Answer me.

==== What does $ mean?
The symbol '$' in Euphoria version 2.5 and later means "length(of_sequence)."

<eucode>
sequence s, i
s = "My Sequence"
i = s[$] -- i = "e"
i = s[4..$] -- i = "Sequence"
i = s[$-5] -- i = "q"
</eucode>

=== 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 [[http://openeuphoria.org/docs/std_sort.html#_2881_sort|sort]]().

==== How can I expand tabs to spaces?
See [[http://openeuphoria.org/docs/std_search.html#_2352_find_replace|find_replace]]() and [[http://openeuphoria.org/docs/std_search.html#_2361_match_replace|match_replace]]().

==== Does Euphoria have function pointers?
see routine_id

==== Does Euphoria have exception handling?
No.

==== How can I move or copy a file?
See [[http://openeuphoria.org/docs/std_filesys.html#_1404_copy_file|copy_file]]() and [[http://openeuphoria.org/docs/std_filesys.html#_1416_move_file|move_file]]().

=== Sequences

==== What's a negative index?
Negative index is a mis-named feature of Python and some other languages. If the index is n, and n = -1,
then this means, in English, "the last". In Euphoria, this would be x[length(x)], or in a more convenient shorthand x[$]. In Python, -2 would mean "next to last". In Euphoria x[length(x)-1] or x[$-1].

==== How do I iterate over a sequence in reverse order?
Use a for..loop, like so

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

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

See [[http://openeuphoria.org/docs/std_sequence.html#_2816_remove_dups|remove_dups]]().

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

<eucode>
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
</eucode>

==== How do you refer to individual elements in an array?
You get the value from a sequence with bracketed values. For instance,

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

==== 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?

See [[http://openeuphoria.org/docs/std_io.html#_1624_filereadingwriting|File Reading/Writing]].

One way to do this is:

# Use [[http://openeuphoria.org/docs/std_io.html#_1626_read_lines|read_lines]]() to read in the contents of a file.
# Manipulate the sequence of lines as desired
# Use [[http://openeuphoria.org/docs/std_io.html#_1641_write_lines|write_lines]]() to write the sequence back to a file.

==== 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 [[http://openeuphoria.org/docs/std_dll.html#_5345_open_dll|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 - for Linux only: [[http://sourceforge.net/projects/eugtk/|EuGTK]]

==== Can I use xforms interfaces in Euphoria?
Answer me.

==== How can I do date arithmetic?
See the [[http://openeuphoria.org/docs/std_datetime.html#_957_datetime|DateTime]] standard library. In particular, [[http://openeuphoria.org/docs/std_datetime.html#_1102_add|add\\() and [[http://openeuphoria.org/docs/std_datetime.html#_1109_subtract|subtract]]().

==== Can I integrate Euphoria and Python?
Answer me.

==== XML parsers
Answer me.

==== Is there a Euphoria library for wxWidgets?
Yes. See Matthew Lewis' [[http://wxeuphoria.sourceforge.net/|wxEuphoria library]].

=== Other Features

==== How can I count the number of lines in a file?
Here's one way:

<eucode>
include std/io.e

sequence fname = "GtkEngine.e"

object lines = read_lines(fname)
printf(1,"There are %d lines in file %s\n",{length(lines),fname})
</eucode>

==== How can I sum the elements in an array?
Here's one way:

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

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