# Creating multiple variables with consecutive names using a for loop

**URL:** <https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 4, 2020, 4:56pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177 "2020-02-04T16:56:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [February 4, 2020, 4:56pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/1 "2020-02-04T16:56:23Z")

</div>

I want to create several variables with consecutive names. For instance, I want to create 100 variables like `variable_1`, `variable_2`, … , `variable_100`, and maybe assign a random vector to each of them, e.g. `variable_1 = rand(1,10)`. Then I want to iterate through each of them and change some elements inside them. How can I do it using a for loop? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 4, 2020, 5:07pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/2 "2020-02-04T17:07:59Z")

</div>

Is there a specific reason why you don’t want to use an array for this?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 4, 2020, 5:15pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/3 "2020-02-04T17:15:01Z")

</div>

You can’t do it. You should use an array.

Or if it’s something static, then you can write a loop to generate the code…

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [February 4, 2020, 5:15pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/4 "2020-02-04T17:15:25Z")

</div>

Do you mean an array of arrays? That’s a good idea. I am coming from MATLAB and was thinking about this problem with MATLAB perspective, where you can do something like `'variable_num2str(i)'`. So you’re saying that’s not possible in Julia?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 4, 2020, 5:17pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/5 "2020-02-04T17:17:08Z")

</div>

Even in MATLAB you should probably use a cell array. I have not tested it but I highly suspect using a cell array is faster.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 4, 2020, 5:32pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/6 "2020-02-04T17:32:13Z")

</div>

Yes, an array of array (or a Matrix, if that’s an option) would be the standard way to achieve this in Julia.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [February 4, 2020, 5:57pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/7 "2020-02-04T17:57:15Z")

</div>

**Although you should use an array** as was suggested, you could technically create and modify variables in the global scope using something like this (maybe it can be written more nicely, **but anyway you probably shouldn’t use it** ):

```julia
# create the arrays
for i in 1:100
    eval(:($(Symbol(:v,"_",i)) = rand(1,10) ))
end

# modify the first element of each array
for i in 1:100
    eval( :($(Symbol(:v,"_",i))[1] = 0 ))
end

```

You can find more info on this in the meta-programming section of the manual. But once again let me emphasize that I don’t think it is necessary for your current use case.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2020, 6:01pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/8 "2020-02-04T18:01:33Z")

</div>

> [@HoldenCaulfield](#):
>
> I am coming from MATLAB and was thinking about this problem with MATLAB perspective, where you can do something like `'variable_num2str(i)'` .

I am also a Matlab user. You should _never_ do this in Matlab either, or in any language. In Matlab you could use a cell array of arrays, for example.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 4, 2020, 6:09pm UTC](https://discourse.julialang.org/t/creating-multiple-variables-with-consecutive-names-using-a-for-loop/34177/9 "2020-02-04T18:09:14Z")

</div>

> [@DNF](#):
>
> or in any language

I wouldn’t say “any language”. I’ve done variance of this in bash and cmake for example. Both of which are very much string processors and lack support for storing nested/structured information. For anything that has truely generic array and map support this shouldn’t be necessary.
