Re: Print a sequence (Phix)
- Posted by petelomax in February
- 501 views
formats in fmt are applied to successive elements.
You've only got one format, and it is applying that to the first element of args. You're just missing some {}.
sequence s = {"line 1", "line 2", "line 3", "line 4"} printf(1, "s: %V\n", {s}) -- extra parenthesis sequence buffer = {} for i = 1 to length(s) do printf(1, "l: %s\n", s[i]) buffer = append(buffer, s[i]) end for printf(1, "buffer1: %v\n", {buffer}) -- {} and %s->%v printf(1, "buffer2: %t\n\n", string(buffer)) -- print a bool
The output is:
s: {"line 1","line 2","line 3","line 4"} l: line 1 l: line 2 l: line 3 l: line 4 buffer1: {"line 1","line 2","line 3","line 4"} buffer2: false
In the first printf(), the first element of args is now all of s, rather than s[1].
Using %s (and the extra {}) in the penultimate printf() would crash with "sequence found in character string".
In your last line, string(buffer) will return false, because it isn't. Also note that in
string name = "lib9" printf(1,"Hello %s\n",name)
It is using a special trick to print Hello lib9, but on Euphoria that would print Hello l, ie just the first character/element of args.
Were you to change that to printf(1,"Hello %s%s\n",name), it would [revert to] printing Hello li, and need another two to get all four chars.
Specifying args as {name} would work the same on both Phix and Euphoria (and crash with the double %s%s).
PS Probably better to post any questions here - its pretty quiet anyway, and others can chip in while I'm akip.