I'm trying to automate some output using printf but I'm struggling to find a way to pass to it the list of arguments expr_1, ..., expr_n
in
printf (dest, string, expr_1, ..., expr_n)
I thought of using something like Javascript's spread
operator but I'm not even sure I should need it.
For instace, say I have a list of strings to be output
a:["foo","bar","foobar"];
a string of appropriate format descriptors, say
s: "~a ~a ~a ~%";
and an output stream, say os
. How can I invoke printf
using these things in such a way that the result will be the same as writing
printf(os,s,a[1],a[2],a[3]);
Then I could generalize it to output lists of variable size.
Any suggestions?
Thanks.
EDIT:
I just learned about apply
and, using the conditions I posed in my OP, the following seems to work wonderfully:
apply(printf,append([os,s],a));