# Indexing a const array with a JuMP variable

**URL:** <https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283>\
**Category:** Optimization (Mathematical)\
**Created:** [July 12, 2019, 10:46am UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283 "2019-07-12T10:46:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 12, 2019, 1:25pm UTC](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283/2 "2019-07-12T13:25:03Z")

</div>

Please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), and provide a minimal working example.

Note that the following will _not_ work:

```nohighlight
model = Model()
@variable(model, x)
data = [1, 2, 3]
@objective(model, data[x]) # Can't index with x.

```

Instead, you should do something like the following:

```nohighlight
model = Model()
data = [1, 2, 3]
@variable(model, x[1:length(data)], Bin)
@constraint(model, sum(x) == 1)
@objective(model, sum(x[i] * data[i] for i in 1:length(data))

```

Here, the binary variables `x` choose each element out of `data`, and there is a constraint that you can only choose one element.

---

_[View the full topic](https://discourse.julialang.org/t/indexing-a-const-array-with-a-jump-variable/26283)._
