# How to extract the indices of a certain variable from a \`Vector{VariableRef}\`?

**URL:** https://discourse.julialang.org/t/how-to-extract-the-indices-of-a-certain-variable-from-a-vector-variableref/113555
**Category:** Optimization (Mathematical)
**Tags:** question, jump, syntax
**Created:** [April 27, 2024, 3:55am UTC](https://discourse.julialang.org/t/how-to-extract-the-indices-of-a-certain-variable-from-a-vector-variableref/113555 "2024-04-27T03:55:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [April 27, 2024, 3:55am UTC](https://discourse.julialang.org/t/how-to-extract-the-indices-of-a-certain-variable-from-a-vector-variableref/113555/1 "2024-04-27T03:55:49Z")

</div>

```julia
using JuMP
m = Model()
@variable(m, x[1:3])
@variable(m, y[1:5])
v = all_variables(m)

```

`v` is an `8-element Vector{VariableRef}`. Now I want to extract all the indices with respect to variable `y` from the vector `v` (e.g., it can be a `Vector{Int64}`: `[4:8;]`). How should I do?

Thanks!

---

<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: [April 27, 2024, 10:27pm UTC](https://discourse.julialang.org/t/how-to-extract-the-indices-of-a-certain-variable-from-a-vector-variableref/113555/2 "2024-04-27T22:27:14Z")

</div>

Do something like:

```Julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x[1:3]);

julia> @variable(model, y[1:5]);

julia> var_to_column = Dict(v => i for (i, v) in enumerate(all_variables(model)))
Dict{VariableRef, Int64} with 8 entries:
  y[1] => 4
  y[5] => 8
  y[2] => 5
  x[2] => 2
  x[1] => 1
  y[4] => 7
  x[3] => 3
  y[3] => 6

julia> [var_to_column[yi] for yi in y]
5-element Vector{Int64}:
 4
 5
 6
 7
 8

```

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [April 28, 2024, 3:21am UTC](https://discourse.julialang.org/t/how-to-extract-the-indices-of-a-certain-variable-from-a-vector-variableref/113555/3 "2024-04-28T03:21:30Z")

</div>

Thanks, @odow ! 🤝 🤝
