# Iterate over a SparseAxisArray container to retrieve variable values and index (jump 0.18+)

**URL:** https://discourse.julialang.org/t/iterate-over-a-sparseaxisarray-container-to-retrieve-variable-values-and-index-jump-0-18/19498
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [January 10, 2019, 10:30pm UTC](https://discourse.julialang.org/t/iterate-over-a-sparseaxisarray-container-to-retrieve-variable-values-and-index-jump-0-18/19498 "2019-01-10T22:30:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![willu47](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@willu47](https://discourse.julialang.org/u/willu47)
#### Post date: [January 10, 2019, 10:30pm UTC](https://discourse.julialang.org/t/iterate-over-a-sparseaxisarray-container-to-retrieve-variable-values-and-index-jump-0-18/19498/1 "2019-01-10T22:30:28Z")

</div>

I wish to iterate over the variables contained in a JuMP.SparseAxisArray so that I can write out variable values using `JuMP.value(var)`, where var is an entry in the array.

I would also like to obtain the value of the index of the variable. So far I’ve tried `var.index` and `JuMP.index(var)` just return an integer, rather than the index set I defined using the `@variable` macro.

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [January 11, 2019, 2:54pm UTC](https://discourse.julialang.org/t/iterate-over-a-sparseaxisarray-container-to-retrieve-variable-values-and-index-jump-0-18/19498/2 "2019-01-11T14:54:51Z")

</div>

You can do it as follows:

```julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x[i=1:2, 1:i])
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Any,Any}} with 3 entries:
  [2, 2] = x[2,2]
  [1, 1] = x[1,1]
  [2, 1] = x[2,1]

julia> for var in x
           @show JuMP.index(var)
       end
JuMP.index(var) = MathOptInterface.VariableIndex(3)
JuMP.index(var) = MathOptInterface.VariableIndex(1)
JuMP.index(var) = MathOptInterface.VariableIndex(2)

```

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [September 4, 2019, 1:44pm UTC](https://discourse.julialang.org/t/iterate-over-a-sparseaxisarray-container-to-retrieve-variable-values-and-index-jump-0-18/19498/3 "2019-09-04T13:44:26Z")

</div>

I have the same question. Before update my JuMP version I was able to sweep the index of my variable easily, like:

```julia
for key in (sort!(collect(keys(var)))) 

```

but I cannot find another way to do it in JuMP 0.9. That’s really bad because my whole set of variables is multi-index (like, [“id”,“region”,“city”] and it’s very important to me be able to sweep the index in many parts of my code.
