Am 23.04.2025 um 10:50 schrieb Artur T.:
I want to have a formatted printing of a string array:
<hansl>
strings S = defarray("A", "B", "C")
printf "My array: %s", S
</hansl>
This results in the error: "Data types not conformable for operation".
The help for the printf command says: "The format %s should be used
for strings." (last sentence in 1st paragraph). I guess that actually
refers to type "string". However, why shouldn't it work for an array
of strings, too?
Well, because a 'strings' array is not a 'string',
hehe - as you knew.
More seriously, it's not obvious how the contained string_s of the array
should be printed, so it cannot "just work". Horizontally, vertically,
other types of separators?
Yes, I could (should?) make use of the flatten() function before
printing, but that does not feel natural (to me). Using the above
code, I would expect a line-by-line printout of the array items.
flatten() is indeed the way to go, I think. There you can specify
exactly the separator, i.e. newline or space (or some other thing).
Actually, if you want to have it line by line, your code above at least
needs an initial \n I'd say. And then it turns out, also at the end. So,
two variants:
<hansl>
strings S = defarray("A", "B", "C")
printf "My array:\n%s\n", flatten(S) # vertical
print "My array:"
printf "%s\n", flatten(S, " ") # horizontal
</hansl>
In the end I don't think that more syntax than that is needed. Note also
the difference to gretl's specialized %m formatting feature for
matrix-es: A matrix is 2-dim, so flatten() would mess that up in
general. In contrast, a strings array is only 1-dim, so no problem.
cheers
sven