March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:
> Is it possible to create such an array in which you can store strings and numbers at the same time?
>
> string-int[] array = [4, "five"];
As there's no mention of performance, what's wrong with a plain old string array with a bit of conversion and error checking?
string[] soup = ["4", "Test", "5", "More Test"];
|
March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On 2015-03-08 21:11:42 +0000, Paul said:
> On Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:
>> Is it possible to create such an array in which you can store strings and numbers at the same time?
>>
>> string-int[] array = [4, "five"];
>
> As there's no mention of performance, what's wrong with a plain old string array with a bit of conversion and error checking?
>
> string[] soup = ["4", "Test", "5", "More Test"];
OP is fighting a loosing battle in flame war on some obscure forum. F# enthusiast trolls OP into solving stupid puzzles that are trivial in F# (or any ML-family language) and clumsy in C-family languages.
In language holy wars the only winning move is not to play.
|
March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Klyga | On Sunday, 8 March 2015 at 21:18:31 UTC, Max Klyga wrote:
> OP is fighting a loosing battle in flame war on some obscure forum. F# enthusiast trolls OP into solving stupid puzzles that are trivial in F# (or any ML-family language) and clumsy in C-family languages.
>
> In language holy wars the only winning move is not to play.
I have not played in a Holy war.
|
March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Klyga | On Sunday, 8 March 2015 at 21:18:31 UTC, Max Klyga wrote:
> On 2015-03-08 21:11:42 +0000, Paul said:
>
>> On Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:
>>> Is it possible to create such an array in which you can store strings and numbers at the same time?
>>>
>>> string-int[] array = [4, "five"];
>>
>> As there's no mention of performance, what's wrong with a plain old string array with a bit of conversion and error checking?
>>
>> string[] soup = ["4", "Test", "5", "More Test"];
>
> OP is fighting a loosing battle in flame war on some obscure forum. F# enthusiast trolls OP into solving stupid puzzles that are trivial in F# (or any ML-family language) and clumsy in C-family languages.
>
> In language holy wars the only winning move is not to play.
Yawn :D
|
March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On 2015-03-08 at 20:26, Meta wrote: > On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote: >> http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this. > > What in the world is that code doing? I'm having a hard time wrapping my head around this. It's a trick to reuse string internals to store an int. A string is a struct with two values (length, ptr). ivalue(i) is used to set ptr = i and length = 0. Except that with this solution you will confuse empty strings with ints. You could give such strings special treatment by replacing: this(string s){ svalue=s; } with: this(string s){ svalue=s; if (!s.length) svalue = cast(string)(cast(char*)0)[X..X]; } // where X is some magic int value to mark that we are dealing with an empty string, you'd still be confused if someone actually wanted to store the X value. |
March 08, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to FG | On Sunday, 8 March 2015 at 21:41:44 UTC, FG wrote: > On 2015-03-08 at 20:26, Meta wrote: >> On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote: >>> http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this. >> >> What in the world is that code doing? I'm having a hard time wrapping my head around this. > > It's a trick to reuse string internals to store an int. > A string is a struct with two values (length, ptr). > ivalue(i) is used to set ptr = i and length = 0. > > Except that with this solution you will confuse empty strings with ints. > You could give such strings special treatment by replacing: > > this(string s){ svalue=s; } > > with: > > this(string s){ svalue=s; if (!s.length) svalue = cast(string)(cast(char*)0)[X..X]; } > // where X is some magic int value to mark that we are dealing with an empty string, > > you'd still be confused if someone actually wanted to store the X value. Oh, I see. What was tripping me up was `svalue=cast(string)(cast(char*)0)[i..i];` But I see now that it's just creating an empty string. |
March 09, 2015 Re: string-int[] array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin Attachments: | On Sun, 08 Mar 2015 18:57:37 +0000, Kagamin wrote:
> http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this.
i hate annoying beginners too, but not to SUCH extent.
|
Copyright © 1999-2021 by the D Language Foundation