On Wed, 23 Apr 2014, Logan Kelly wrote:
Hello,
Question about strings and string substitution. Why does option 1 yield
different output from option 2 (see below). I am using 1.9.14 on Win 7
64bit
hansel:
#option 1
s = "1\n2\n3\n"
printf "@s"
#option 2
printf "1\n2\n3\n"
OUTPUT:
gretl version 1.9.14
Current session: 2014-04-22 23:49
#option 1
? s = "1\n2\n3\n"
Replaced string s
? printf "@s"
1\n2\n3\n
#option 2
? printf "1\n2\n3\n"
1
2
3
That's by design. The two sister funtions/commands printf and sprintf
support ASCII control characters, simple assignment does not. If you need
to embed a newline or a tab into a string, use sprintf. For example:
<hansl>
sprintf a "%d\t%d", 3, 4
print a
</hansl>
That said, we are considering the idea to turn the sprintf command into a
function (this is already available in CVS and the snapshots), so perhaps
you may want to experiment with the new notation, which would be
scalar n = sprintf(a, "%d\t%d", 3, 4)
where n is the number of characters that "a" contains.
-------------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Scienze Economiche e Sociali (DiSES)
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
-------------------------------------------------------