Jump to page: 1 2
Thread overview
string-int[] array
Mar 08, 2015
Dennis Ritchie
Mar 08, 2015
Baz
Mar 08, 2015
Baz
Mar 08, 2015
Dennis Ritchie
Mar 08, 2015
Dennis Ritchie
Mar 08, 2015
Meta
Mar 08, 2015
Baz
Mar 08, 2015
Kagamin
Mar 08, 2015
Dennis Ritchie
Mar 08, 2015
Meta
Mar 08, 2015
FG
Mar 08, 2015
Meta
Mar 10, 2015
Kagamin
Mar 09, 2015
ketmar
Mar 08, 2015
Paul
Mar 08, 2015
Max Klyga
Mar 08, 2015
Dennis Ritchie
Mar 08, 2015
Paul
March 08, 2015
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"];
March 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)]
March 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:
>> 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 ?
March 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);
> }
> ----
>
>> [Tuple!(string, int)("a", 65), Tuple!(string, int)("b", 66)]

Thanks, will do.
March 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"]
March 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:
>> 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");
}
March 08, 2015
http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this.
March 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:
>> 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.
March 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:
>> 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.
March 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.
« First   ‹ Prev
1 2