www.digitalmars.com         C & C++   DMDScript  

D - String in Multidim.Dynam.Array

reply antome <antome_member pathlink.com> writes:
How can I put a String in a Multi Dimensional Dynamic Array ?

char[][] myarray;
char[]   mystring;

mystring = "abc...";  // Works!!
myarray = split(anystring);
mystring = myarray[0]; // Works!

myarray[0] = "abc...";  // Works NOT !
myarray[0][] = "abc...";  // Works NOT !  ArrayBoundsError

antome
Nov 27 2003
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <bq571t$18eq$1 digitaldaemon.com>, antome says...
How can I put a String in a Multi Dimensional Dynamic Array ?

char[][] myarray;
char[]   mystring;

mystring = "abc...";  // Works!!
myarray = split(anystring);
mystring = myarray[0]; // Works!

myarray[0] = "abc...";  // Works NOT !
myarray[0][] = "abc...";  // Works NOT !  ArrayBoundsError

antome
myarray has length = 0 so myarray[0] is out of bounds "~=" will append to a dinamic array so it's very simple: myarray ~= "abc..."; Ant
Nov 27 2003
prev sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
char [] [] thanksgiving;

thanksgiving ~= "turkey";
thanksgiving ~= "tort";
thanksgiving ~= "footbal";

However ive been playing with it , and Im not sure how to delete the 2nd
element, and have the array resize so that length is 2 and it contains
"turkey" and "football"

delete thanksgiving[1];
thanksgiving[1] = null;
thanksgiving.length = 2;

not working

you can assign them manually of course, im missing something ?
C








"antome" <antome_member pathlink.com> wrote in message
news:bq571t$18eq$1 digitaldaemon.com...
 How can I put a String in a Multi Dimensional Dynamic Array ?

 char[][] myarray;
 char[]   mystring;

 mystring = "abc...";  // Works!!
 myarray = split(anystring);
 mystring = myarray[0]; // Works!

 myarray[0] = "abc...";  // Works NOT !
 myarray[0][] = "abc...";  // Works NOT !  ArrayBoundsError

 antome
Nov 27 2003
next sibling parent reply Ant <Ant_member pathlink.com> writes:
In article <bq5d3q$1hk0$1 digitaldaemon.com>, Charles Sanders says...
char [] [] thanksgiving;

thanksgiving ~= "turkey";
thanksgiving ~= "tort";
thanksgiving ~= "footbal";

However ive been playing with it , and Im not sure how to delete the 2nd
element, and have the array resize so that length is 2 and it contains
"turkey" and "football"

delete thanksgiving[1];
thanksgiving[1] = null;
thanksgiving.length = 2;
Don't do: thanksgiving[1..thanksgiving.length-1] = thanksgiving[2..thanksgiving.length]; thanksgiving.length = thanksgiving.length-1; The compiler will accept that (and actualy runs fine on some DMD versions) but the manual says it illegal. inserting with the same slicing technic will not run as expected. you must cycle through the array to shift the elements. do the same thing to insert. but, of course, arrays aren't suppose to be used when you need to delete and inserting elements frequently. I guess that's why Walter doesn't want to implement the "smart" overlap slicing. Ant
Nov 27 2003
parent "Charles Sanders" <sanders-consulting comcast.net> writes:
Then we need a template library ( having to eat my words earlier of, 'we
dont need no stinking STL' ).

Hmmmm.

C

"Ant" <Ant_member pathlink.com> wrote in message
news:bq5f9s$1l4d$1 digitaldaemon.com...
 In article <bq5d3q$1hk0$1 digitaldaemon.com>, Charles Sanders says...
char [] [] thanksgiving;

thanksgiving ~= "turkey";
thanksgiving ~= "tort";
thanksgiving ~= "footbal";

However ive been playing with it , and Im not sure how to delete the 2nd
element, and have the array resize so that length is 2 and it contains
"turkey" and "football"

delete thanksgiving[1];
thanksgiving[1] = null;
thanksgiving.length = 2;
Don't do: thanksgiving[1..thanksgiving.length-1] =
thanksgiving[2..thanksgiving.length];
 thanksgiving.length = thanksgiving.length-1;

 The compiler will accept that (and actualy runs fine on some DMD versions)
 but the manual says it illegal.
 inserting with the same slicing technic will not run as expected.

 you must cycle through the array to shift the elements.
 do the same thing to insert.

 but, of course, arrays aren't suppose to be used when you need to
 delete and inserting elements frequently. I guess that's why
 Walter doesn't want to implement the "smart" overlap slicing.

 Ant
Nov 27 2003
prev sibling parent Hauke Duden <H.NS.Duden gmx.net> writes:
Charles Sanders wrote:
 char [] [] thanksgiving;
 
 thanksgiving ~= "turkey";
 thanksgiving ~= "tort";
 thanksgiving ~= "footbal";
 
 However ive been playing with it , and Im not sure how to delete the 2nd
 element, and have the array resize so that length is 2 and it contains
 "turkey" and "football"
 
 delete thanksgiving[1];
 thanksgiving[1] = null;
 thanksgiving.length = 2;
 
 not working
 
 you can assign them manually of course, im missing something ?
 C
I think delete only works for associative arrays, not regular ones. So what you're really doing is deleting the string object itself, not the array slot. So I guess you're stuck with manually moving the elements down. [ This is another example why delete shouldn't be used to delete slots in associative arrays. It just soooo inconsistent and confusing. We need a different operator/method for deleting array slots!] Hauke
Nov 27 2003