multidimensional array setting/slicing?

Steven Schveighoffer schveiguy at yahoo.com
Sat Jul 26 13:56:38 PDT 2008


"Steven Schveighoffer" wrote
> "Rakan Alhneiti" wrote
>> Saaa wrote:
>>> Is there a easy way to set/read a multidimensional slice of an array?
>>>
>>> I'd like to do something like this... :)
>>>
>>> int array[][];
>>> array.length=100;
>>> array[].length=200; :  Error: slice expression array[] is not a 
>>> modifiable lvalue
>>>
>>> int array2[][];
>>> array2.length=4;
>>> array2[].length=2;
>>> array2=[[1,2,3,4],[5,6,7,8]];
>>>
>>> array[10..10+array2.length][150..150+array2[].length]=array2;
>> I dont think you can do this in any programming language not just D.
>> Initialization of inner arrays in multidimensional arrays is done most of 
>> the type using loops
>> int array[][];
>> array.length = 100;
>> for(int i = 0;i<array.length;i++)
>> {
>> array[i].length = 4;
>> }
>
> Probably done easiest with a foreach loop:
>
> foreach(ref a; array)
>    a.length = 4;
>
> The slicing is somthing that would have to be done using loops.
>
> you might not realize, but a multi-dimensional dynamic array is really an 
> array of pointers, not one contiguous block of memory.  So each sub-array 
> points to a different memory space, and could have different lengths.
>
> a loop with foreach would look something like:
>
> foreach(i, ref a; array[10..10+array2.length])
>    a[] = array2[i];

err.. that should have been
a[150..150+array2[i].length] = array2[i];

-Steve 




More information about the Digitalmars-d-learn mailing list