# Getting JuMP.Variable from variable name "x\[1\]"

**URL:** <https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 15, 2017, 8:38pm UTC](https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875 "2017-07-15T20:38:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 15, 2017, 8:38pm UTC](https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875/1 "2017-07-15T20:38:15Z")

</div>

Is there a straightforward way to get a JuMP.Variable object from the model using the variable name or column index that does not involve the relatively inefficient `eval(parse(vname))`?

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [July 15, 2017, 8:46pm UTC](https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875/2 "2017-07-15T20:46:13Z")

</div>

Maybe I misunderstand your question, but you can get variables (and named constraints, I believe) by indexing into the model object:

```julia
julia> m = Model(); @variable(m, y)
y

julia> m[:y]
y

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 15, 2017, 8:47pm UTC](https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875/3 "2017-07-15T20:47:10Z")

</div>

No you didn’t 🙂

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [July 15, 2017, 8:52pm UTC](https://discourse.julialang.org/t/getting-jump-variable-from-variable-name-x-1/4875/4 "2017-07-15T20:52:29Z")

</div>

This also falls under relatively inefficient because it involves a dictionary lookup and the type of the result cannot be inferred.

[http://www.juliaopt.org/JuMP.jl/0.17/refvariable.html#constructors](http://www.juliaopt.org/JuMP.jl/0.17/refvariable.html#constructors)

> The constructor `Variable(m::Model,idx::Int)` may be used to create a variable object corresponding to an existing variable in the model (the constructor does not add a new variable to the model).
