www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array Concatenate

reply "Paul" <phshaffer gmail.com> writes:
If this works...

D programming book section 4.1.9 Expanding
auto a = [87, 40, 10];
a ~= 42;
assert(a== [87, 40, 10, 42]);

why doesnt' this work?

DeletedBlks ~= matchOld[0];

the dmd compiler comes back with
Error: cannot append type string to type string[ulong]

Does this append operator only work for literals?
Jun 08 2012
next sibling parent reply "Paul" <phshaffer gmail.com> writes:
On Friday, 8 June 2012 at 12:41:13 UTC, Paul wrote:
 If this works...

 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

 why doesnt' this work?

 DeletedBlks ~= matchOld[0];

 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]

 Does this append operator only work for literals?

Jun 08 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 08.06.2012 16:42, Paul wrote:
 On Friday, 8 June 2012 at 12:41:13 UTC, Paul wrote:
 If this works...

 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

 why doesnt' this work?

 DeletedBlks ~= matchOld[0];

 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]

 Does this append operator only work for literals?


meaningfully ? AA with integer keys != plain arrays -- Dmitry Olshansky
Jun 08 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/08/2012 07:50 AM, Paul wrote:

 string[ulong] is a standard array
 ulong[string] would be associative....NO?

No, they are both associative arrays. string[ulong] is a mapping from ulong to string, ulong[string] is a mapping from string to ulong. Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jun 08 2012
prev sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 08.06.2012 18:50, Paul wrote:
 On Friday, 8 June 2012 at 12:50:56 UTC, Dmitry Olshansky wrote:
 On 08.06.2012 16:42, Paul wrote:
 On Friday, 8 June 2012 at 12:41:13 UTC, Paul wrote:
 If this works...

 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

 why doesnt' this work?

 DeletedBlks ~= matchOld[0];

 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]

 Does this append operator only work for literals?


meaningfully ? AA with integer keys != plain arrays

string[ulong] is a standard array ulong[string] would be associative....NO?

While I see logic, but no. And the reason is that associative means non-contiguous, that is deletedBlks[0] is not next to deletedBlks[1] etc. More over there could be gaps.. And most important there is no order of elements. It's just a map: given integer - give a string. And plain arrays are more then that :) -- Dmitry Olshansky
Jun 08 2012
prev sibling next sibling parent "Paul" <phshaffer gmail.com> writes:
On Friday, 8 June 2012 at 12:50:56 UTC, Dmitry Olshansky wrote:
 On 08.06.2012 16:42, Paul wrote:
 On Friday, 8 June 2012 at 12:41:13 UTC, Paul wrote:
 If this works...

 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

 why doesnt' this work?

 DeletedBlks ~= matchOld[0];

 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]

 Does this append operator only work for literals?


"appending" meaningfully ? AA with integer keys != plain arrays

string[ulong] is a standard array ulong[string] would be associative....NO?
Jun 08 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, June 08, 2012 14:41:10 Paul wrote:
 If this works...
 
 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

That compiles just fine.
 why doesnt' this work?
 
 DeletedBlks ~= matchOld[0];
 
 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]
 
 Does this append operator only work for literals?

You didn't give us the types of either DeletedBlks or matchOld[0]. However, from the error, it looks like DeletedBlks is a string[ulong], and matchOld[0] is a string. string[ulong] is an associate array - which means that it's a hash table - and appending doesn't make any sense with an associative array, because AAs aren't ordered. They take key-value pairs. So, something like DeletedBlks[42] = matchOld[0]; should compile - which creates the key-value pair of 42 and the value of matchOld[0]. Section 4.4 of TDPL discusses associative arrays (which you obviously aren't far from, since you're referring to section 4.1.9). So, read that section, and things should be clearer. - Jonathan M Davis
Jun 08 2012
prev sibling next sibling parent "Nathan M. Swan" <nathanmswan gmail.com> writes:
On Friday, 8 June 2012 at 12:41:13 UTC, Paul wrote:
 If this works...

 D programming book section 4.1.9 Expanding
 auto a = [87, 40, 10];
 a ~= 42;
 assert(a== [87, 40, 10, 42]);

 why doesnt' this work?

 DeletedBlks ~= matchOld[0];

 the dmd compiler comes back with
 Error: cannot append type string to type string[ulong]

 Does this append operator only work for literals?

It seems like you're confusing _associative_ arrays with _regular_ arrays; they are different things. A regular array is written as T[], or an array of Ts. An associative array is written as K[V], or a map from K to V. If you have a list of something, use string[], not string[ulong].
Jun 08 2012
prev sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Friday, 8 June 2012 at 18:49:42 UTC, Nathan M. Swan wrote:
 It seems like you're confusing _associative_ arrays with 
 _regular_ arrays; they are different things.

 A regular array is written as T[], or an array of Ts.
 An associative array is written as K[V], or a map from K to V.

 If you have a list of something, use string[], not 
 string[ulong].

I'd consider the Associative Array with any numeric key as a sparse array; Course like that you wouldn't get them in any particular order... And most of the time you wouldn't need to. I've found all kinds of interesting uses for numeric based AA's, one of them is one of my examples of an expanding prime number-generating range, another could be for a COW (Copy-on-write) based array; Reminds me I'll write a struct for one if there isn't one yet.
Jun 08 2012