Thread overview
Abra-kah-dah-brah! Turn into a string ( std.string.toString() no working right...?)
Mar 08, 2005
jicman
Mar 09, 2005
Lukas Pinkowski
Mar 09, 2005
Regan Heath
March 08, 2005
I will enlighten you all with this very complicated piece of d code:

import std.stdio;
import std.string;
char[] TurnToMonDigit(char[] mon)
{
char[] mm;
int i;
static char [3][12] sMon = ["Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"];
for (int i=0;i<sMon.length;i++)
{
if(std.string.cmp(sMon[i],mon) == 0)
{
writef(sMon[i] ~ " " ~ mon ~ " ");
writef(i);
writef(" ");
break;
}
}
if(i > 11)
{
writefln("*** Bad Date found: " ~ mon ~ " ***");
return(null);
}
mm = std.string.toString(i+1);
mm = std.string.zfill(mm,2);
writef(mm ~ "\n");
return(mm);
}
void main (char[][] args)
{
char[] d;
d = TurnToMonDigit("Jan");
d = TurnToMonDigit("Feb");
d = TurnToMonDigit("Nov");
d = TurnToMonDigit("Dec");
}

Go ahead and compile it. :-) Then run it. :-) I get:

18:47:54.95>test
Jan Jan 0 01
Feb Feb 1 01
Nov Nov 10 01
Dec Dec 11 01

Huh?! :-|  Isn't i suppose to be i+1?  This is probably hidden in one of d's technical writings, but I missed it.  Anyone care to explain it?

thanks.

josé


March 09, 2005
jicman wrote:
> import std.stdio;
> import std.string;
> char[] TurnToMonDigit(char[] mon)
> {
> char[] mm;
> int i;

This i is initialized to 0.

> static char [3][12] sMon = ["Jan","Feb","Mar","Apr","May","Jun",
> "Jul","Aug","Sep","Oct","Nov","Dec"];
> for (int i=0;i<sMon.length;i++)
> {
> if(std.string.cmp(sMon[i],mon) == 0)
> {
> writef(sMon[i] ~ " " ~ mon ~ " ");
> writef(i);


This i hides the other int i; thus, when Month is Jan, i is 0, for Feb it's 1 a.s.o

> writef(" ");
> break;
> }
> }

Here the scope of the for(int i) is over, now we have the other i, which was
intialized to 0.

> if(i > 11)
> {
> writefln("*** Bad Date found: " ~ mon ~ " ***");
> return(null);
> }
> mm = std.string.toString(i+1);

mm will be always "1", as i is initialized to 0, and you add 1 to it ;-)


Greetings,

Lukas
March 09, 2005
On Wed, 09 Mar 2005 01:35:15 +0100, Lukas Pinkowski <Lukas.Pinkowski@web.de> wrote:
> jicman wrote:
>> import std.stdio;
>> import std.string;
>> char[] TurnToMonDigit(char[] mon)
>> {
>> char[] mm;
>> int i;
>
> This i is initialized to 0.
>
>> static char [3][12] sMon = ["Jan","Feb","Mar","Apr","May","Jun",
>> "Jul","Aug","Sep","Oct","Nov","Dec"];
>> for (int i=0;i<sMon.length;i++)
>> {
>> if(std.string.cmp(sMon[i],mon) == 0)
>> {
>> writef(sMon[i] ~ " " ~ mon ~ " ");
>> writef(i);
>
>
> This i hides the other int i; thus, when Month is Jan, i is 0, for Feb it's
> 1 a.s.o

Do we want an error for this? (I think I do)
Can we get an error for this?
Walter?

Regan