# How to include the iteration number of a loop in variable names?

**URL:** <https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 26, 2022, 7:23am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840 "2022-11-26T07:23:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_ricci](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)\
**Post date:** [November 26, 2022, 7:23am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840/1 "2022-11-26T07:23:45Z")

</div>

I have a loop iterating on r and want to define the variable x^r in every iteration.

I know one possibility is defining an anonymous variable, but it is not what I need.

```julia
m=Model()
for r in 1:4
      x = @variable(m, [r:r] )
end

```

I need the iteration number to be included in the variable name, so in the end of the iteration I’ll have: x^1 , x^2 , x^3 and x^4.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 26, 2022, 7:42am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840/2 "2022-11-26T07:42:34Z")

</div>

```julia
m=Model()
x = map(1:4) do r
      @variable(m, [r:r] )
end

```

Then you have a vector `x` with `x[1]` to `x[4]`.

---

<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:** [November 27, 2022, 12:50am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840/3 "2022-11-27T00:50:18Z")

</div>

Are you looking for something like:

```julia
model = Model()
x = VariableRef[]
for r in 1:4
    x_r = @variable(model, base_name = "x[$r]")
    push!(x, x_r)
end

```

Docs: [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Variable-names)

---

<div class="post-metadata">

**Author:** ![Alex\_ricci](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)\
**Post date:** [May 18, 2023, 6:55am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840/4 "2023-05-18T06:55:07Z")

</div>

Thank for the your help.  
I define this variable

```julia
δ = VariableRef[]
        for t in 1:T
            for ω in 1:Ω
                for l in 1:L[t,ω]
                    for r in 1:R
                        δ_r = @variable(m, base_name = "δ[$t,$ω,$l,$r]")
                        push!(δ, δ_r)
                    end
                end
            end
        end

```

and then this constraint

```julia
        l1_r1 = @constraint(m, [t in 1:T, ω in 1:Ω, r in 1:1], β[t,ω] ≥ 
                                     sum( δ[t,ω,l,r] for l in 1:L[t,ω] )  
            )

```

and get this error

```julia
BoundsError: attempt to access 9-element Vector{VariableRef} at index [1, 1, 2, 1]

```

Which I suppose means `y[1,1,2,1]` is not available.  
When I print `y` I see `y[1,1,2,1]`is there

```julia
VariableRef[δ[1,1,1,1], δ[1,1,2,1], δ[1,1,3,1], δ[1,1,4,1], δ[1,2,1,1], δ[1,2,2,1], δ[1,2,3,1], δ[1,2,4,1], δ[1,2,5,1]]

```

Can somebody please help what I am missing?

---

<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:** [May 18, 2023, 7:19am UTC](https://discourse.julialang.org/t/how-to-include-the-iteration-number-of-a-loop-in-variable-names/90840/5 "2023-05-18T07:19:29Z")

</div>

`δ` is a vector with 9 elements. You can only index it with `δ[1]` through `δ[9]`.

I think you want instead:

```plaintext
@variable(m, δ[t=1:T, ω=1:Ω, 1:L[t,ω], 1:R])

```
