Re: Euphoria

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Sat, 13 Nov 1999, you wrote:
> Hi!
>
> I'm a PC programmer. I am fluent in BASIC, and have experimented with many
> other languages but BASIC has been usually best suited to my needs.  I found
> Euphoria and tried it out, and it is <EM>really</EM> cool!  (Pardon my
> HTML.)  I need some help, though, using Euphoria.  The syntax of modifying
> variables, being somewhat different from BASIC, kindof confused me at first.
>   If anyone can help me (i.e. send some demoish stuff) please do.

There are really only three kinds of variable in Euphoria:
integers (you know about them)
   1, 57, 204003, -7.....
atoms (called reals in Pascal, floats in C, I forget what in Basic)
   1.99, 3.14159, -66
You do math with these in the usual way, i = i * 4, x = x / 2.....

sequences (either strings of characters, or lists of numbers, or both, mixed)
s = {2,5,7,9,-3}

math happens on each item in a sequence with one call:
s * 2
s will now contain: {4,10,14,18,-6}

coords = { {1,1}, {10,10}, {30,15}, {30,30} }  -- a list of coordinates
coords += 10 -- move the coordinates to the right and down by  10
now coords contains: { {11,11}, {20,20}, {40,25}, {40,40} }

customer =  {"James Brown", "156 Maple Rd.", "Macon, GA", 46, "555-1234"}
here, customer[1] is "James Brown"
        customer[3] is "Macon, GA"
You can move James to Florida by assigning customer[3] = "Panama City, FL"

sequences can be arbitrarily complex. Suppose you need to save info on the
entire Brown family:
customer = {"James Brown",46,"156 Maple Rd.", "Macon, GA","555-1234",
                    {{"Velma",42}, {"Barry",16}, {"Lucy",14}} }

Now you can store info on James, his wife and kids in one variable.
customer[6] is a list of James' dependents.
customer[6][1] is his wife's name and age {"Velma",43}
customer[6][3] is his daughter's name and age {"Lucy",14}

customer[6][2][2] will be 16 (Barry's age)

length(customer[6]) will return 3 = which is the number of items stored in that
sequence (i.e. the number of dependents)

If James had no dependents, the sixth item in customer would be an empty set of
curly braces: {}  to denote an empty sequence.

If James and Velma have another kid (Keisha) you can add her to the list as
follows:  customer[6] = append(customer[6], {"Keisha",1})

Of couse, all this [6][1][2] stuff gets hard to follow, so it's better to set
up some constants so you can refer to things by name, not by number:
constant NAME = 1, AGE = 2, ADDR = 3,  DEPENDENTS = 6

printf(1,"%s is %d years old\n",{ customer[NAME], customer[AGE]}) will print:
James Brown is 46 years old

for i = 1 to length(customer[DEPENDENTS]) do
  printf(1,"%s is %d years old\n", {customer[DEPENDENTS][i]} )
end for

will print:
Velma 42
Barry 16
Lucy 14
Keisha 1

Complicated? Yes, but pretty powerful.

Regards,
Irv Mullins

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu