Questions about the slice operator

Jacob Carlborg doob at me.com
Wed Apr 4 07:53:27 PDT 2012


On 2012-04-04 16:40, Simen Kjærås wrote:
> It's quite simple, really - an index set holds indices. For a regular
> array of N elements, the index set it [0..N-1]. For an AA, the index set
> is all the keys in the AA. Basically, an index set is the set of all
> values that will give meaningful results from container[index].
>
> arr[2..4] thus means 'restrict the indices to those between 2 and 4'.
> For arrays though, it also translates the array so that what was 2
> before, now is 0.
>
> For a T[string] aa, one could imagine the operation aa["a".."c"] to
> produce a new AA with only those elements whose keys satisfy
> "a" <= key < "c".
>
> As for the example given:
>
> assert(['a','b','c'][[0,2]] == ['a', 'c']);
>
> This means 'grab the elements at position 0 and 2, and put them in
> a new array'. Hence, element 0 ('a') and element 2 ('c') are in
> the result.

Ok, now I think I get it.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list