I spoke to soon in my earlier message -- the problem now seems even stranger.
The attached script embeds the string substitution part into the loops that generate the
vector of dates. Now it seems that the substitution works mostly fine -- the following
sequence gets generated in the loops:
1986.05
1986.06
1986.07
1986.08
1986.09
1986.1
1986.11
1986.12
1987.01
1987.02
1987.03
So the "10" is still not behaving properly. But the vector that gets built comes
out like this:
1986.0
1986.0
1986.1
1986.1
1986.1
1986.1
1986.1
1986.1
1986.1
1987.0
1987.0
1987.0
Very strange...
PS
-----Original Message-----
From: gretl-users-bounces(a)lists.wfu.edu [mailto:gretl-users-bounces@lists.wfu.edu] On
Behalf Of Allin Cottrell
Sent: Friday, June 14, 2013 9:39 AM
To: Gretl list
Subject: Re: [Gretl-users] bug in string substitution?
On Thu, 13 Jun 2013, Summers, Peter wrote:
I'm trying to generate a matrix of "dates" that I can
use on the
x-axis of graphs of forecasts. I want the format to be "yyyy.q" or
"yyyy.mm" for quarterly or monthly data, respectively. Things work
fine for quarterly data, but something weird happens with monthly
data.
I create a string with the desired format, then make the corresponding
matrix entry using string substitution. So for example the string
"2009.04" should just become the "number"
2009.04. However, the string substitution results in the second
"decimal" being truncated, so I get 2009.0 instead of 2009.04, or
2009.1 instead of 2009.12.
The attached script illustrates the problem.
Hmm, I'm not seeing a problem here (current CVS on Linux). But note that the
recommended way to get a month to appear in the desired format with sprintf is to use the
conversion "%02d"
(which inserts a zero if and only if it's needed).
If I do
<hansl>
scalar yr = 2009
scalar mo = 4
string s
sprintf s, "%d.%02d", yr, mo
printf "s = '%s'\n", s
scalar d = @s
</hansl>
I get:
<output>
? scalar yr = 2009
Generated scalar yr = 2009
? scalar mo = 4
Generated scalar mo = 4
? string s
? sprintf s, "%d.%02d", yr, mo
s = '2009.04'
? scalar d = 2009.04
Generated scalar d = 2009.04
</output>
Allin Cottrell
_______________________________________________
Gretl-users mailing list
Gretl-users(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users