1. Beginner Question
- Posted by Rich Klender <rklender at e?cite.c?m> Jul 24, 2007
- 484 views
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).
with trace trace(1) atom a sequence Var1, Var2, Var3 Var1 = "apple" Var2 = "orange" Var3 = "File" printf(1, "Var1 equals: %s\n", {Var1}) printf(1, "Var2 equals: %s\n", {Var2}) Var3 = append(Var3, Var1) printf(1, "Var3 after append equals: %s\n", Var3) printf(1, "Var31 after append equals: %s\n", Var3[1]) printf(1, "Var32 after append equals: %s\n", Var3[2]) Var3 = prepend(Var3, Var2) printf(1, "Var3 after prepend equals: %s\n", Var3) a=getc(0) trace(0)
This 1st printf's work fine, the next one where Var3 is printed out after the append gives "F", the 1st letter of "File". The next printf gives "F" again, the next gives "i", the last "orange". When running trace, the variables are assigned correctly, i.e. Var3 gives "appleFile" then "appleFileorange". What am I doing wrong? Also, in the Euphoria Reference Manual, under printf, it states when using printf to watch out for: printf(1, "%s", name) and to use {name} instead. However, this always gives a error when I tried it on Var3, taking these out, the program ran fine, well, almost fine, other then it's not outputting correctly!! Any help would be appreciated!! Thanks Rich
2. Re: Beginner Question
- Posted by Derek Parnell <ddparnell at bi?p?nd.com> Jul 24, 2007
- 460 views
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