Re: Beginner Question

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

Rich Klender wrote:
> 
> Hello all!
> 
> I was fooling around with some simple concepts and cannot get the following
> to
> print out correctly (in the Dos window, I'm using Winxp).

Ok, I'll annotate and comment as we go ...

with trace
trace(1)
atom a
sequence Var1, Var2, Var3

Var1 = "apple" 
-- Var1 now contains a 5-element sequence 'a', 'p', 'p', 'l', and 'e'
 
Var2 = "orange" 
-- Var2 now contains a 6-element sequence 'o', 'r', 'a', 'n', 'g', and 'e' 
 
Var3 = "File"
-- Var3 now contains a 4-element sequence 'F', 'i', 'l', and 'e'

printf(1, "Var1 equals: %s\n", {Var1})
-- The '%s' means that the next ELEMENT of the third parameter
-- is displayed as a string. The third parameter is a 1-element
-- sequence {"apple"}. By placing the {} around the Var1 you are
-- creating a 1-element sequence whose only element is the 
-- variable itself. Because '%s' is the first (and only) formatting
-- token, the next element is "apple" so that's what gets printed out.

-- printf takes exactly three parameters. The third parameter must be
-- either a single number (integer or atom) or a single sequence. 

printf(1, "Var2 equals: %s\n", {Var2})


Var3 = append(Var3, Var1)

-- The append() function adds ONE element, the second parameter
-- to the end of the first parameter and returns the result.
-- So in this case Var3 is now 5-element sequence containing
-- 'F', 'i', 'l', 'e', and "apple". Note that the last element
-- is a sequence (or sub-sequence if you like).

-- I guess what you were trying for was to concatenate the two 
-- strings rather than append. In that case you would use ...
-- Var3 = Var3 & Var1

printf(1, "Var3 after append equals: %s\n", Var3)

-- Remember that the first '%s' will act on the first element
-- in the third parameter of printf. In this case, the first
-- element is 'F'.

printf(1, "Var31 after append equals: %s\n", Var3[1])

-- Remember that the third parameter of printf can be either a sequence
-- or a number. In euphoria, strings are really sequences of integers.
-- In this case, Var3[1] is the first element of Var3 which is 'F'.

printf(1, "Var32 after append equals: %s\n", Var3[2])
-- This prints out 'i' which is the second element of Var3.

Var3 = prepend(Var3, Var2)

-- prepend() adds one element (parameter 2) to the start of the first
-- parameter. So in this case, Var3 will now be a 6-element sequence 
-- containing ... "orange", 'F', 'i', 'l', 'e', and "apple".

printf(1, "Var3 after prepend equals: %s\n", Var3)

-- Again, the first '%s' acts on the first element. The first element 
-- is now "orange" so that's what you see printed.

-- If you were trying concatenate these try this instead ...
-- Var3 = Var2 & Var3

-- append()/prepend() always adds ONE element to the sequence.
-- concatenation adds all the elements together.


-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

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

Search



Quick Links

User menu

Not signed in.

Misc Menu