Thread overview
Array Concatenate
Jun 08, 2012
Paul
Re: Array Append
Jun 08, 2012
Paul
Jun 08, 2012
Dmitry Olshansky
Jun 08, 2012
Paul
Jun 08, 2012
Ali Çehreli
Jun 08, 2012
Dmitry Olshansky
Jun 08, 2012
Jonathan M Davis
Jun 08, 2012
Nathan M. Swan
Jun 09, 2012
Era Scarecrow
June 08, 2012
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?
June 08, 2012
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?


June 08, 2012
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?
>
because it's associative array and it doesn't support "appending" meaningfully ?

AA with integer keys != plain arrays


-- 
Dmitry Olshansky
June 08, 2012
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?
>>
> because it's associative array and it doesn't support "appending" meaningfully ?
>
> AA with integer keys != plain arrays

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


June 08, 2012
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

June 08, 2012
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
June 08, 2012
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?
>>>
>> because it's associative array and it doesn't support "appending"
>> 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
June 08, 2012
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].
June 09, 2012
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.