1. Print a sequence (Phix)
- Posted by lib9 in February
- 480 views
Hi,
This would be more suitable for a IRC chat, but I couldn't find any Euphoria channel.
At printf documentation it's said that If args is a sequence, then formats in fmt are applied to successive elements. and the definition is:
printf(integer fn, string fmt, object args={})
Please, consider this:
sequence s = {"line 1", "line 2", "line 3", "line 4"} printf(1, "s: %V\n", s) 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: %s\n", buffer) printf(1, "buffer2: %s\n\n", string(buffer))
The output is:
- s: "line 1"
l: line 1
l: line 2
l: line 3
l: line 4
buffer1: line 1
buffer2:
- #
- s: "line 1"
But I was expecting that the line buffer1:... would print all the sequence s, but only prints the first element.
To print a sequence, one has to make use of a for or while loop?
Am I interpreting the above instructions in a wrong way?
2. Re: Print a sequence (Phix)
- Posted by petelomax in February
- 479 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.
3. Re: Print a sequence (Phix)
- Posted by jimcbrown (admin) in February
- 466 views
Hi,
This would be more suitable for a IRC chat, but I couldn't find any Euphoria channel.
I'm not sure that there is one since freenode imploded.
But I was expecting that the line buffer1:... would print all the sequence s, but only prints the first element.
To print a sequence, one has to make use of a for or while loop?
Am I interpreting the above instructions in a wrong way?
The message is there because strings are sequences. So the common mistake is to do something C-like,
printf(1, "%s\n", "hello world!")
and wonder why only the h was printed. It's explaining that it's one format per element, a one to one pairing that increments on both sides in parallel.
Basically you only have one %s in the printf, so it will only print out the first element of the sequence. You need one %s per element to print out multiple string values in a single printf.
4. Re: Print a sequence (Phix)
- Posted by ghaberek (admin) in February
- 426 views
This would be more suitable for a IRC chat, but I couldn't find any Euphoria channel.
I'm not sure that there is one since freenode imploded.
We have a Discord server: https://discord.gg/urQF5JyCrV. It's not terribly active but more having people around would help with that. I'm hooked into several Discord servers so I'm almost always online.
-Greg
5. Re: Print a sequence (Phix)
- Posted by katsmeow in February
- 434 views
Hi,
This would be more suitable for a IRC chat, but I couldn't find any Euphoria channel.
I'm not sure that there is one since freenode imploded.
[09:24:am] * Connecting to irc.eu.libera.chat (6667) ...... . 09:25:13:am 02/06/2024 Tuesday katsmeow joined #Phix . ...... . 09:25:36:am 02/06/2024 Tuesday katsmeow joined #OpenEuphoria . ...... . 09:25:48:am 02/06/2024 Tuesday katsmeow joined #euphoria .
That said, no one joins #OpenEuphoria or #Phix , and no one talks in #euphoria. But the channels exist. The channels have been there for years, formed the same day FreeNode collapsed. Ever feel like collateral damage? I mean, you need something, and everyone's decision is against you?
Kat
6. Re: Print a sequence (Phix)
- Posted by lib9 in February
- 388 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
Thank you very much, Pete. I though that s and buffer were already declared as sequences so it wasn't needed to use the pair of braces in the printf statement.
Regarding Discord, I don't use as they ask for a phone number for identification and I don't want to give them that.
7. Re: Print a sequence (Phix)
- Posted by jimcbrown (admin) in February
- 394 views
Regarding Discord, I don't use as they ask for a phone number for identification and I don't want to give them that.
You can sign up with a throwaway email IIRC.
That said, no one joins #OpenEuphoria or #Phix , and no one talks in #euphoria. But the channels exist. The channels have been there for years, formed the same day FreeNode collapsed.
Good to know. Wonder who's in the latter channel nowadays.
Ever feel like collateral damage? I mean, you need something, and everyone's decision is against you?
Kat
To be fair, that sums up pretty much all of freenode nowadays.
Forked into: Regarding Discord and Verification