Thanks Allin, this works beautifully! (of course ;-))
I saw a post in the archives about the functional form of sprintf, but it looks like
it's changed a bit since then.
PS
-----Original Message-----
From: gretl-users-bounces(a)lists.wfu.edu [mailto:gretl-users-
bounces(a)lists.wfu.edu] On Behalf Of Allin Cottrell
Sent: Tuesday, April 12, 2016 8:33 PM
To: Gretl list
Subject: Re: [Gretl-users] string substitution with sprintf?
On Tue, 12 Apr 2016, Summers, Peter wrote:
> Hi all,
>
> I'm trying to write a script that will produce a LaTeX table as output -
basically a customized version of tabprint. Here's the basic idea:
> <hansl>
> matrix doh = mnormal(1,3) # this would be a coefficient vector, e.g.
>
> string fmt = "%.4g "
> string baz = "doh[1]"
> loop i=2..cols(doh) # build up the format and argument strings
> fmt ~= " & %.4g "
> baz ~= ", doh[$i]"
> endloop
> fmt
> baz
>
> sprintf Foo "\"%s\"\n", fmt
> Foo
>
> # this printf is what I want, but I want to use the "fmt" string above
> printf "%.4g & %.4g & %.4g\n", @baz
>
> # this seems like it should work, but I get a "syntax error"
> sprintf bar Foo, @baz
> </hansl>
I guess it's time to replace the doc for the "sprintf" command with a
"deprecated" statement plus pointer to the sprintf() function, which is now
the recommended form.
<hansl>
matrix doh = mnormal(1,3)
string fmt = "%.4g "
string args = "doh[1]"
loop i=2..cols(doh) -q
fmt ~= " & %.4g "
args ~= ", doh[$i]"
endloop
# assuming you want a terminating newline fmt ~= "\n"
Foo = sprintf(fmt, @args)
Foo
</hansl>
We recommend, these days, that string substitution be kept to a minimum,
but the "args" argument requires it in context, so OK.
Allin
_______________________________________________
Gretl-users mailing list
Gretl-users(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users