May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 17 May 2015 at 09:24:19 UTC, anonymous wrote:
> On Sunday, 17 May 2015 at 09:20:17 UTC, Dennis Ritchie wrote:
>> On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>>> auto s = cast(char[][])["foo", "bar"];
>>
>> Thanks. This version I was completely satisfied.
>
> Remember that Daniel Kozak wrote "if you are sure thats what you really need". I'm confident that you're not sure it's what you need. For starters, this crashes on linux:
>
> ----
> auto s = cast(char[][])["foo", "bar"];
> s[1][1] = 't';
> ----
And no crashes on Windows :)
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 17 May 2015 at 09:21:58 UTC, Marc Schütz wrote:
> On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>>
>> On Sun, 17 May 2015 09:06:38 +0000
>> Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
>> wrote:
>>
>>> Hi,
>>> It seems to me, or D do not create mutable array of strings?
>>>
>>> How to create a mutable equivalent of a string array?
>>>
>>> -----
>>> string[] s = ["foo", "bar"];
>>> // s[1][1] = 't'; // immutable expression s[1][1]
>>
>> or you can use cast if you are sure thats what you really need:
>> auto s = [cast(char[])"foo", cast(char[])"bar"];
>>
>> or
>>
>> auto s = cast(char[][])["foo", "bar"];
>
> That's not a good idea. I haven't checked, but this will likely segfault on mutation
Yep, you are right, shame on me :)
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Sunday, 17 May 2015 at 09:26:15 UTC, Dennis Ritchie wrote:
> And no crashes on Windows :)
Yeah, on windows it's even worse.
void main()
{
auto s = cast(char[][])["foo", "bar"];
s[1][1] = 't';
import std.stdio;
writeln("bar");
}
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Sunday, 17 May 2015 at 09:20:17 UTC, Dennis Ritchie wrote:
> On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>> auto s = cast(char[][])["foo", "bar"];
>
> Thanks. This version I was completely satisfied.
So maybe this one would be ok with you too :)
auto s = to!(char[][])(["foo", "bar"]);
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | I remembered code Ali Çereli. It really helped: http://forum.dlang.org/thread/ulhtlyxxclihaseefrot@forum.dlang.org#post-mihl6m:241che:241:40digitalmars.com ----- import std.stdio, std.traits, std.range, std.algorithm; auto deepDup(A)(A arr) if (isArray!A) { static if (isArray!(ElementType!A)) { return arr.map!(a => a.deepDup).array; } else { return arr.dup; } } void main() { auto s = ["foo", "bar"].deepDup; s[1][1] = 't'; writeln(s); } ----- http://rextester.com/QBFH12695 P.S. Need to enable deepDup in Phobos. |
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 17 May 2015 at 09:36:33 UTC, anonymous wrote:
> On Sunday, 17 May 2015 at 09:26:15 UTC, Dennis Ritchie wrote:
>> And no crashes on Windows :)
>
> Yeah, on windows it's even worse.
>
> void main()
> {
> auto s = cast(char[][])["foo", "bar"];
> s[1][1] = 't';
> import std.stdio;
> writeln("bar");
> }
Yes exactly.
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Sunday, 17 May 2015 at 09:37:56 UTC, Daniel Kozak wrote:
> So maybe this one would be ok with you too :)
>
> auto s = to!(char[][])(["foo", "bar"]);
Now it works :)
|
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Sun, 17 May 2015 09:39:21 +0000 Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I remembered code Ali Çereli. It really helped: http://forum.dlang.org/thread/ulhtlyxxclihaseefrot@forum.dlang.org#post-mihl6m:241che:241:40digitalmars.com > > ----- > import std.stdio, std.traits, std.range, std.algorithm; > > auto deepDup(A)(A arr) > if (isArray!A) > { > static if (isArray!(ElementType!A)) { > return arr.map!(a => a.deepDup).array; > > } else { > return arr.dup; > } > } > > void main() { > > auto s = ["foo", "bar"].deepDup; > > s[1][1] = 't'; > > writeln(s); > } > ----- > http://rextester.com/QBFH12695 > > P.S. Need to enable deepDup in Phobos. allready there: auto s = ["foo", "bar"].map!"a.dup".array; :) |
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Sunday, 17 May 2015 at 09:57:05 UTC, Daniel Kozak wrote: > > On Sun, 17 May 2015 09:39:21 +0000 > Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> > wrote: > >> I remembered code Ali Çereli. It really helped: >> http://forum.dlang.org/thread/ulhtlyxxclihaseefrot@forum.dlang.org#post-mihl6m:241che:241:40digitalmars.com >> >> ----- >> import std.stdio, std.traits, std.range, std.algorithm; >> >> auto deepDup(A)(A arr) >> if (isArray!A) >> { >> static if (isArray!(ElementType!A)) { >> return arr.map!(a => a.deepDup).array; >> >> } else { >> return arr.dup; >> } >> } >> >> void main() { >> >> auto s = ["foo", "bar"].deepDup; >> >> s[1][1] = 't'; >> >> writeln(s); >> } >> ----- >> http://rextester.com/QBFH12695 >> >> P.S. Need to enable deepDup in Phobos. > > allready there: > auto s = ["foo", "bar"].map!"a.dup".array; > > :) Ouch ignore this one :D |
May 17, 2015 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Sunday, 17 May 2015 at 10:05:34 UTC, Daniel Kozak wrote:
> Ouch ignore this one :D
Yes, it will not work with multidimensional arrays :)
|
Copyright © 1999-2021 by the D Language Foundation