www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array collection ?

reply GG <g g.com> writes:
Actually I use an associative array with name as index, to contain different
data but it allows only string type. It forces me to cast all time.
ex: 
string[char[]][int] a;

a[0]["Month"] = "Jan";
a[0]["Profit"] = to!string(1000); // when I want use this as int, I use :
to!int()
a[0]["Expenses"] = "6000"; // when I want use this as int, I use : to!int()

But as you can see, "Profit" and "Expenses" should be int or double to avoid
cast when I want calculate these fields.

It works with cast but I suppose it's slower than use exact type.

I would like to know if it's possible to have a associative array with multiple
type as a collection with phobos.

Thanks !
Feb 09 2010
next sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
GG wrote:
 Actually I use an associative array with name as index, to contain different
data but it allows only string type. It forces me to cast all time.
 ex: 
 string[char[]][int] a;
 
 a[0]["Month"] = "Jan";
 a[0]["Profit"] = to!string(1000); // when I want use this as int, I use :
to!int()
 a[0]["Expenses"] = "6000"; // when I want use this as int, I use : to!int()
 
 But as you can see, "Profit" and "Expenses" should be int or double to avoid
cast when I want calculate these fields.
 
 It works with cast but I suppose it's slower than use exact type.
 
 I would like to know if it's possible to have a associative array with
multiple type as a collection with phobos.
 
 Thanks !
Questions like this belong in the d.learn group. If you're using D2, you can use Variant. If you're using D1, and you don't want to use Tango, there's std.boxer.
Feb 09 2010
prev sibling next sibling parent reply Steve Teale <steve.teale britseyeview.com> writes:
On Wed, 10 Feb 2010 01:16:53 -0500, GG wrote:

 I would like to know if it's possible to have a associative array with
 multiple type as a collection with phobos.
You could try std.boxer, or std.variant.
Feb 09 2010
parent GG <g g.com> writes:
Sorry for posting at wrong place.
And thank you for your responses. I will try it and learn it !
Feb 10 2010
prev sibling parent reply torhu <no spam.invalid> writes:
On 10.02.2010 07:16, GG wrote:
 Actually I use an associative array with name as index, to contain different
data but it allows only string type. It forces me to cast all time.
 ex:
 string[char[]][int] a;

 a[0]["Month"] = "Jan";
 a[0]["Profit"] = to!string(1000); // when I want use this as int, I use :
to!int()
 a[0]["Expenses"] = "6000"; // when I want use this as int, I use : to!int()

 But as you can see, "Profit" and "Expenses" should be int or double to avoid
cast when I want calculate these fields.

 It works with cast but I suppose it's slower than use exact type.

 I would like to know if it's possible to have a associative array with
multiple type as a collection with phobos.
Are you sure it's not simpler to just use a struct? struct Foo { int month; // or char[] month, or an enum double profit; double expenses; }
Feb 12 2010
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
torhu wrote:
 ...
 
 struct Foo
 {
   int month;  // or char[] month, or an enum
   double profit;
   double expenses;
 }
Bad torhu: never use doubles for currency values!
Feb 12 2010