www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative Array Bug?

reply jicman <cabrera_ _wrc.xerox.com> writes:
I have this program,

import std.stdio;
void main()
{
  char[][] t;
  t = [
        "ProjID",
        "id",
        "parent",
        "children",
        "login",
        "cust",
        "proj",
        "class",
        "bdate",
        "ddate",
        "edate",
        "pm",
        "lang",
        "vendor",
        "invoice",
        "ProjFund",
        "A_No",
        "notes",
        "status"
          "should give error"
      ];
  foreach (char[] s; t)
    writefln(s);
  writefln(t.length);
}

and it compiles and runs.  However, I forgot a comma after a status and so the
last two strings get concatenated by the program. ie.

19:46:18.54>testarr
ProjID
id
parent
children
login
cust
proj
class
bdate
ddate
edate
pm
lang
vendor
invoice
ProjFund
A_No
notes
statusshould give error
19

Is this the correct behaviour?  Should not the compiler protest about a
missing comma or a lack of ~?

back to D'ing...

thanks.

josé
Jan 29 2007
next sibling parent "Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> writes:
It is a feature:
http://www.digitalmars.com/d/lex.html
From section "String Literals":

	The following are all equivalent:

	"ab" "c"
	r"ab" r"c"
	r"a" "bc"
	"a" ~ "b" ~ "c"
	\x61"bc"
Jan 29 2007
prev sibling next sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
jicman escribió:
 I have this program,
 
 import std.stdio;
 void main()
 {
   char[][] t;
   t = [
         "ProjID",
         "id",
         "parent",
         "children",
         "login",
         "cust",
         "proj",
         "class",
         "bdate",
         "ddate",
         "edate",
         "pm",
         "lang",
         "vendor",
         "invoice",
         "ProjFund",
         "A_No",
         "notes",
         "status"
           "should give error"
       ];
   foreach (char[] s; t)
     writefln(s);
   writefln(t.length);
 }
 
 and it compiles and runs.  However, I forgot a comma after a status and so the
 last two strings get concatenated by the program. ie.
 
 19:46:18.54>testarr
 ProjID
 id
 parent
 children
 login
 cust
 proj
 class
 bdate
 ddate
 edate
 pm
 lang
 vendor
 invoice
 ProjFund
 A_No
 notes
 statusshould give error
 19
 
 Is this the correct behaviour?  Should not the compiler protest about a
 missing comma or a lack of ~?
 
 back to D'ing...
 
 thanks.
 
 jos�
That is the correct behaviour. Check this: http://digitalmars.com/d/lex.html#StringLiteral "Adjacent strings are concatenated with the ~ operator, or by simple juxtaposition." Although now it seems to me that this "feature" is kind of buggy... isn't it?
Jan 29 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Ary,
 That is the correct behaviour. Check this:
 
 http://digitalmars.com/d/lex.html#StringLiteral
 
 "Adjacent strings are concatenated with the ~ operator, or by simple
 juxtaposition."
 
 Although now it seems to me that this "feature" is kind of buggy...
 isn't it?
 
That comes from C which does this (most likely to make up for the lack of ~) and make things like this easy "Error in file: "__FILE__"\n" or for a more D like case writef(\r\n);
Jan 29 2007
prev sibling parent =?UTF-8?B?SmFyaS1NYXR0aSBNw6RrZWzDpA==?= <jmjmak utu.fi.invalid> writes:
Ary Manzana wrote:
 "Adjacent strings are concatenated with the ~ operator, or by simple
 juxtaposition."
 
 Although now it seems to me that this "feature" is kind of buggy...
 isn't it?
Maybe syntax highlighting might help catch these. Otherwise strings in D are very handy since you don't have to mess with too many "s and 's.
Jan 30 2007
prev sibling parent Lionello Lunesu <lio lunesu.remove.com> writes:
jicman wrote:
 I have this program,
 
 import std.stdio;
 void main()
 {
   char[][] t;
That's a normal array, by the way, not an associative array. L.
Jan 30 2007