Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 27, 2003 String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
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 |
November 27, 2003 Re: String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to antome | 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 |
November 27, 2003 Re: String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to antome | 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 |
November 27, 2003 Re: String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | 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 |
November 27, 2003 Re: String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | 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 > > |
November 27, 2003 Re: String in Multidim.Dynam.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | 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
|
Copyright © 1999-2021 by the D Language Foundation