Search code examples
arraysminizinc

How to get the first (oder last) n elements of a minizinc array with 1 dimension?


Sounds quite straightforward, but I didn't find a solution yet. How do I get the first (oder last) n elements of a minizinc array with one dimension? The result should be an array, so that I can apply count () oder sum().


Solution

  • The easiest way if you have an array arr with an index set of 1..m is the following array comprehension:

    [ arr[i] | i in 1..n ]
    

    You can also construct an array comprehension that does not depend on the index set starting from 1:

    [ arr[i] | i in (min(index_set(arr)))..(min(index_set(arr)))+n ]
    

    Note that either comprehension will not work if length(arr) < n.