www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - arrays in DMD V2

reply "steven kladitis" <steven_kladitis yahoo.com> writes:
import std.stdio;


void main()
{

int    a0[];
int    a1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as above????
int    a5[][string][string][string];
int    a6[string][int][string][float];
int    a7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???



}

//help please
Apr 18 2014
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 19 April 2014 at 00:27:32 UTC, steven kladitis wrote:
 import std.stdio;


 void main()
 {

 int    a0[];
 int    a1[][];
 string a2[][];
 string a3[][string];
 string a4[][string][string];
 //string a4[string][string][string]; is this the same as 
 above????
 int    a5[][string][string][string];
 int    a6[string][int][string][float];
 int    a7[int][int][string];

 // how do you initialize each array, what type are they 
 multidimensional???
 // how do you define all of the above with limits on each 
 dimension???
 }

 //help please
First, don't use that syntax. everything should be placed on the type: Second, void main() { int a5[][string][string][string]; pragma(msg, typeof(a5)); } Third, http://dlang.org/arrays.html http://dlang.org/hash-map.html
Apr 18 2014
parent reply "steven kladitis" <steven_kladitis yahoo.com> writes:
Thanks, I am trying to understand what I am doing. The docs seem 
unclear to me on how to initialize these.  I think there are 
three ways.
  with brackets , the other with foreach or direct assignments.
-- here is a longer version
-- uncomment out the assignments to see the errors I get.
-- I am trying to understand arrays.



On Saturday, 19 April 2014 at 01:13:55 UTC, Jesse Phillips wrote:
 On Saturday, 19 April 2014 at 00:27:32 UTC, steven kladitis 
 wrote:
 import std.stdio;


 void main()
 {

 int    a0[];
 int    a1[][];
 string a2[][];
 string a3[][string];
 string a4[][string][string];
 //string a4[string][string][string]; is this the same as 
 above????
 int    a5[][string][string][string];
 int    a6[string][int][string][float];
 int    a7[int][int][string];

 // how do you initialize each array, what type are they 
 multidimensional???
 // how do you define all of the above with limits on each 
 dimension???
 }

 //help please
First, don't use that syntax. everything should be placed on the type: Second, void main() { int a5[][string][string][string]; pragma(msg, typeof(a5)); } Third, http://dlang.org/arrays.html http://dlang.org/hash-map.html
Apr 18 2014
next sibling parent reply "steven kladitis" <steven_kladitis yahoo.com> writes:
import std.stdio;


void main()
{

int    a0[];
int    a1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as above????
int    a5[][string][string][string];
int    a6[string][int][string][float];
int    a7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???

a0  = [1,2,3];
//writefln( a0 );

a1  = [ [ 1,2,3 ],[4,5,6]];
//writefln( a1 );

a2  = [ ["a","b" ],[ "c","d" ] ];

//writefln ( a2 );

//a3  = [ ["a","b" ],[ "c","d" ] ];
// does not work

//a4    = [ ["a","b" ];
// does not work.

//a5      = [ [1,"a","b"]];

   pragma(msg, typeof(a0));
    pragma(msg, typeof(a1));
     pragma(msg, typeof(a2));
      pragma(msg, typeof(a3));
       pragma(msg, typeof(a4));
        pragma(msg, typeof(a5));
         pragma(msg, typeof(a6));
          pragma(msg, typeof(a7));
//  does not work

//a6        = [ 1,["a",1,"b",4.0]];
// does not work


//a7 = [ 1,1,"a"];
// does not work


// also how would you do in a foreach?



}
Apr 18 2014
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 19 April 2014 at 03:51:02 UTC, steven kladitis wrote:
 import std.stdio;


 void main()
 {

 int    a0[];
 int    a1[][];
 string a2[][];
 string a3[][string];
 string a4[][string][string];
 //string a4[string][string][string]; is this the same as 
 above????
 int    a5[][string][string][string];
 int    a6[string][int][string][float];
 int    a7[int][int][string];

 // how do you initialize each array, what type are they 
 multidimensional???
 // how do you define all of the above with limits on each 
 dimension???

 a0  = [1,2,3];
 //writefln( a0 );

 a1  = [ [ 1,2,3 ],[4,5,6]];
 //writefln( a1 );

 a2  = [ ["a","b" ],[ "c","d" ] ];

 //writefln ( a2 );

 //a3  = [ ["a","b" ],[ "c","d" ] ];
 // does not work

 //a4    = [ ["a","b" ];
 // does not work.

 //a5      = [ [1,"a","b"]];

   pragma(msg, typeof(a0));
    pragma(msg, typeof(a1));
     pragma(msg, typeof(a2));
      pragma(msg, typeof(a3));
       pragma(msg, typeof(a4));
        pragma(msg, typeof(a5));
         pragma(msg, typeof(a6));
          pragma(msg, typeof(a7));
 //  does not work
Works for me. a6 = int[float][string][int][string] a7 = int[string][int][int]
 //a6        = [ 1,["a",1,"b",4.0]];
 // does not work
a6 = ["a": [1: ["b": [4.0: 5]]]]; writeln(a6["a"]);
 //a7 = [ 1,1,"a"];
 // does not work
a7 = [1: [2: ["3": 4]]];
 // also how would you do in a foreach?
foreach is for getting values out. a0 ~= 7 // Append 7 to array int[string] a8; a8["hello"] = 6; // Add six to a8 for "hello" assert(a8["hello"] == 6);
Apr 18 2014
parent reply "steven kladitis" <steven_kladitis yahoo.com> writes:
void main()
{

//int    a0[];
int[] a0;
//int    a1[][];
int[][] a1;
//string a2[][];
string[][] a2;
//string a3[][string];
string[string] a3;
//  string[][string] a3;
// possibly should be above for a3
//string a4[][string][string];
string[][string][string] a4;
//string a4[string][string][string]; is this the same as above????
//int    a5[][string][string][string];
int[][string][string][string] a5;
//int    a6[string][int][string][float];
int[string][int][string][float] a6;
//int    a7[int][int][string];
int[int][int][string] a7;


// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???

a0  = [1,2,3];
// works
writeln( a0 );

a1  = [ [ 1,2,3 ],[4,5,6]];
// works
writeln( a1 );

a2  = [ ["a","b" ],[ "c","d" ] ];
//works
writeln ( a2 );

a3  = [ "a":"b","c":"d"];
// works
writeln ( a3 );

a4    = [ "a":["b":["c" ]]];
//works
writeln( a4 );

//a5    = [ 1 :["a":["b":["c" ]]]];
// does not work
writeln(a5);

//  pragma(msg, typeof(a0));
//  pragma(msg, typeof(a1));
//  pragma(msg, typeof(a2));
//  pragma(msg, typeof(a3));
//  pragma(msg, typeof(a4));
//  pragma(msg, typeof(a5));
//  pragma(msg, typeof(a6));
//  pragma(msg, typeof(a7));


//a6        = [ 1,["a",1,"b",4.0]];
// does not work
//a6 = ["a": [1: ["b": [4.0: 5]]]];
// does not work
writeln(a6);


//a7 = [ 1,1,"a"];
// does not work
//a7 = [1: 2[:"3"]];
// does not work
writeln(a7);


// in DMD 2.065
// not sure how to initialize 5,6,7



}
Apr 19 2014
next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 19 April 2014 at 16:14:45 UTC, steven kladitis wrote:
 void main()
 {

 //int    a0[];
 int[] a0;
 //int    a1[][];
 int[][] a1;
 //string a2[][];
 string[][] a2;
 //string a3[][string];
 string[string] a3;
 //  string[][string] a3;
 // possibly should be above for a3
 //string a4[][string][string];
 string[][string][string] a4;
 //string a4[string][string][string]; is this the same as 
 above????
 //int    a5[][string][string][string];
 int[][string][string][string] a5;
 //int    a6[string][int][string][float];
 int[string][int][string][float] a6;
 //int    a7[int][int][string];
 int[int][int][string] a7;
These are not the same [a3-a7], please double check what the pragma prints for your types and what you have written here for conversion.
Apr 19 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/19/2014 09:14 AM, steven kladitis wrote:

 // does not work
I think you are thinking in terms of C's syntax. Unlike C, array definition syntax is consistent in D. I inserted spaces below to make it stand out: int[] [string] arr; The definition above is in the following form: ValueType[KeyType] arr; So, it is an associative array of string keys and int[] values. If you think that way, the following program makes sense: void main() { int[][string][string][string] a5; int[string][int][string][float] a6; int[int][int][string] a7; a5 = [ "a" : [ "b" : [ "c" : [ 1 ] ] ] ]; a6 = [ 1.0f : [ "a" : [ 1 : [ "b": 5 ] ] ] ]; a7 = [ "a" : [ 1 : [ 2 : 3 ]]]; } Ali
Apr 19 2014
parent reply "steven kladitis" <steven_kladitis yahoo.com> writes:
Fantastic!!!!

I am getting a much clearer picture of arrays!!!! I appreciate 
all of the help!!!!
I see many examples of D online, but most do not compile under 
2.065.  I am wondering if there is a D manual or book just for D 
V2.0.
Apr 19 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/19/2014 11:59 AM, steven kladitis wrote:

 I am getting a much clearer picture of arrays!!!!
Great! :)
 a D manual or book just for D V2.0.
They are all here: http://wiki.dlang.org/Books Ali
Apr 19 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2014 08:47 PM, steven kladitis wrote:
 Thanks, I am trying to understand what I am doing. The docs seem unclear
 to me on how to initialize these.  I think there are three ways.
   with brackets , the other with foreach or direct assignments.
 -- here is a longer version
 -- uncomment out the assignments to see the errors I get.
 -- I am trying to understand arrays.
I like to differentiate between arrays and associative arrays. Although its name has "array" in it, an associative arrays is actually a hash table. First, let me repeat Jesse Phillips's suggestion: Do not use the C syntax. Here is the D syntax: T[N] means "an array of N objects of type T." So, the following is an array of 2 ints: int[2] arr; When the size of the array is specified like that, it is a fixed-length array. There are dynamic arrays where the actual array is maintained and owned by the runtime. Such arrays are accessed by a slice. A slice is defined similar to a fixed-length array but the size is missing: int[] arr; An associative array (AA) is defined by the syntax ValueType[KeyType]. So, the following is an AA mapping strings to doubles: double[string] table; Here are three chapters on these topics: http://ddili.org/ders/d.en/arrays.html http://ddili.org/ders/d.en/slices.html http://ddili.org/ders/d.en/aa.html Ali
Apr 18 2014