Hi Sven,
Am 23.04.25 um 13:48 schrieb Sven Schreiber:
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?
True, but one could just decide to print the array in some way.
Personally, I would go for "vertically".
>
> 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>
Yes, my (not explicitly mentioned before) point is that this is not so
obvious for (most?) users, I would assume. You have to know about the
flatten() function just for a formatted printing of a string array. I
think that can be simplified.
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.
In the best case, there is no need to have another syntactic sugar. By
the way, for a matrix (also an array) you can use the following which
works for any numeric data type (apart from "matrices", I think):
<hansl>
matrix m = mnormal(3,4)
printf "%12.2f", m
</>
Best,
Artur