digitalmars.D.learn - string-int[] array
- Dennis Ritchie (3/3) Mar 08 2015 Is it possible to create such an array in which you can store
- Baz (14/18) Mar 08 2015 using an array of tuple it works:
- Baz (4/23) Mar 08 2015 mmmh maybe off-topic, you probably don't what pairs but either a
- Dennis Ritchie (6/10) Mar 08 2015 No, will not work.
- Dennis Ritchie (2/14) Mar 08 2015 Thanks, will do.
- Kagamin (1/1) Mar 08 2015 http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this.
- Dennis Ritchie (4/68) Mar 08 2015 Thanks.
- Meta (3/4) Mar 08 2015 What in the world is that code doing? I'm having a hard time
- FG (11/14) Mar 08 2015 It's a trick to reuse string internals to store an int.
- ketmar (2/3) Mar 09 2015 i hate annoying beginners too, but not to SUCH extent.=
- Paul (4/7) Mar 08 2015 As there's no mention of performance, what's wrong with a plain
- Max Klyga (5/15) Mar 08 2015 OP is fighting a loosing battle in flame war on some obscure forum. F#
- Dennis Ritchie (2/7) Mar 08 2015 I have not played in a Holy war.
- Paul (2/19) Mar 08 2015 Yawn :D
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"];
Mar 08 2015
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"];using an array of tuple it works: ---- import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Mar 08 2015
On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote:On Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:mmmh maybe off-topic, you probably don't what pairs but either a string representing an int or an int, do you ? If so then an array of union ?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"];using an array of tuple it works: ---- import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Mar 08 2015
On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote:Thanks, will do.No, will not work. On Sunday, 8 March 2015 at 18:25:33 UTC, Baz wrote:mmmh maybe off-topic, you probably don't what pairs but either a string representing an int or an int, do you ? If so then an array of union ?string-int[] array; a = [5, "v", 4, "t", "a", "b", 7, 9, 10, 15, "example"]; writeln(a); // [5, "v", 4, "t", "a", "b", 7, 9, 10, 15, "example"]
Mar 08 2015
On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote:import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----Thanks, will do.[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Mar 08 2015
On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote:On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote:It might be better to use std.variant.Algebraic. An array of tuples is wasteful of memory as you only need one or the other. import std.variant; alias IntOrStr = Algebraic!(int, string); IntOrStr[] makeIntOrStrArray(T...)(T vals) { import std.algorithm; import std.array; auto result = new IntOrStr[](T.length); foreach (i, val; vals) { result[i] = IntOrStr(val); } return result; } void main() { IntOrStr[] arr = makeIntOrStrArray(4, "five"); }import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----Thanks, will do.[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Mar 08 2015
On Sunday, 8 March 2015 at 18:54:43 UTC, Meta wrote:On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote:Yes, but the tuple is used here because i misunderstood the question, cf my own answer to my first answer, anyway, never mind.On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote:It might be better to use std.variant.Algebraic. An array of tuples is wasteful of memory as you only need one or the other. import std.variant; alias IntOrStr = Algebraic!(int, string); IntOrStr[] makeIntOrStrArray(T...)(T vals) { import std.algorithm; import std.array; auto result = new IntOrStr[](T.length); foreach (i, val; vals) { result[i] = IntOrStr(val); } return result; } void main() { IntOrStr[] arr = makeIntOrStrArray(4, "five"); }import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----Thanks, will do.[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Mar 08 2015
On Sunday, 8 March 2015 at 18:54:43 UTC, Meta wrote:On Sunday, 8 March 2015 at 18:38:02 UTC, Dennis Ritchie wrote:Thanks. On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote:On Sunday, 8 March 2015 at 18:18:15 UTC, Baz wrote:It might be better to use std.variant.Algebraic. An array of tuples is wasteful of memory as you only need one or the other. import std.variant; alias IntOrStr = Algebraic!(int, string); IntOrStr[] makeIntOrStrArray(T...)(T vals) { import std.algorithm; import std.array; auto result = new IntOrStr[](T.length); foreach (i, val; vals) { result[i] = IntOrStr(val); } return result; } void main() { IntOrStr[] arr = makeIntOrStrArray(4, "five"); }import std.stdio; import std.typecons; alias T = Tuple!(string, int); void main(string[] args) { T[] tarr; tarr ~= T("a",65); tarr ~= T("b",66); writeln(tarr); } ----Thanks, will do.[Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this. struct IntString { string svalue; this(string s){ svalue=s; } this(int i){ ivalue=i; } int ivalue() const { assert(svalue.length==0); return cast(int)svalue.ptr; } void ivalue(int i) { svalue=cast(string)(cast(char*)0)[i..i]; } } int main() { auto s=IntString(5); assert(s.ivalue==5); s.ivalue=-6; assert(s.ivalue==-6); return 0; }Thanks.
Mar 08 2015
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.
Mar 08 2015
On 2015-03-08 at 20:26, Meta wrote:On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote: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.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.
Mar 08 2015
On Sunday, 8 March 2015 at 21:41:44 UTC, FG wrote:On 2015-03-08 at 20:26, Meta wrote: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.On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote: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.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.
Mar 08 2015
On Sunday, 8 March 2015 at 21:41:44 UTC, FG wrote:Except that with this solution you will confuse empty strings with ints.The idea was to only make it memory-safe without union.
Mar 10 2015
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.=
Mar 09 2015
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"];
Mar 08 2015
On 2015-03-08 21:11:42 +0000, Paul said:On Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:(or any ML-family language) and clumsy in C-family languages. In language holy wars the only winning move is not to play.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"];
Mar 08 2015
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 C-family languages. In language holy wars the only winning move is not to play.I have not played in a Holy war.
Mar 08 2015
On Sunday, 8 March 2015 at 21:18:31 UTC, Max Klyga wrote:On 2015-03-08 21:11:42 +0000, Paul said:Yawn :DOn Sunday, 8 March 2015 at 18:05:33 UTC, Dennis Ritchie wrote:OP is fighting a loosing battle in flame war on some obscure C-family languages. In language holy wars the only winning move is not to play.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"];
Mar 08 2015