www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string-int[] array

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
next sibling parent reply "Baz" <bb.temp gmx.com> writes:
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
next sibling parent reply "Baz" <bb.temp gmx.com> writes:
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:
 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)]
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 ?
Mar 08 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
prev sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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);
 }
 ----

 [Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Thanks, will do.
Mar 08 2015
parent reply "Meta" <jared771 gmail.com> writes:
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:
 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)]
Thanks, will do.
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"); }
Mar 08 2015
parent "Baz" <bb.temp gmx.com> writes:
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:
 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);
 }
 ----

 [Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Thanks, will do.
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"); }
Yes, but the tuple is used here because i misunderstood the question, cf my own answer to my first answer, anyway, never mind.
Mar 08 2015
prev sibling next sibling parent reply "Kagamin" <spam here.lot> writes:
http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this.
Mar 08 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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:
 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);
 }
 ----

 [Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]
Thanks, will do.
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"); }
Thanks. On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote:
 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
prev sibling next sibling parent reply "Meta" <jared771 gmail.com> writes:
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
parent reply FG <home fgda.pl> writes:
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.
Mar 08 2015
next sibling parent "Meta" <jared771 gmail.com> writes:
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.
Mar 08 2015
prev sibling parent "Kagamin" <spam here.lot> writes:
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
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
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
prev sibling parent reply "Paul" <paul example.com> writes:
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
parent reply Max Klyga <max.klyga gmail.com> writes:
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.
Mar 08 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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.
Mar 08 2015
prev sibling parent "Paul" <paul example.com> writes:
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
Mar 08 2015