# How to simply this without writing a loop?

**URL:** <https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 19, 2021, 5:14pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337 "2021-12-19T17:14:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 19, 2021, 5:14pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/1 "2021-12-19T17:14:05Z")

</div>

For example, I have 4 parameters: A, B, C, and D, each representing an observed property. Each stores a matrix with the size of 360 x 180.

Below is my loop:

```julia
Vars = ["A", "B", "C", "D"];
for i in Vars

   if i == "A"
   Z = A;
   elseif i == "B"
   Z = B;
   elseif i == "C"
   Z = C
   elseif i == "D"
   Z = D;
   end

end

```

For my if loop, is it possible to archive that without writing a loop? After all, the name of the Matrix is equal to the name of the variable under each of the 4 conditions.

Many thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 19, 2021, 5:19pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/2 "2021-12-19T17:19:12Z")

</div>

> [@leon](#):
>
> For example, I have 4 parameters: A, B, C, and D, each representing an observed property. Each stores a matrix with the size of 360 x 180.

Consider using a different data structure? e.g. use a dictionary to store these parameters.

It’s hard to give sensible advice without more context, because the code you posted does exactly nothing — it would have no effect if you deleted the loop completely.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 19, 2021, 5:23pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/3 "2021-12-19T17:23:42Z")

</div>

Thanks, Steven!

The goal of my if loop is to assign the respective matrix to Z.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 19, 2021, 5:26pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/4 "2021-12-19T17:26:20Z")

</div>

Perhaps something like:

```julia
julia> A=[1 2 ; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> s="A"
"A"

julia> Z=eval(Symbol(s))
2×2 Matrix{Int64}:
 1 2
 3 4

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 19, 2021, 5:31pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/5 "2021-12-19T17:31:42Z")

</div>

> [@oheil](#):
>
> `Z=eval(Symbol(s))`

It works! 😀

That’s exactly what I’m looking for. Thank you so much.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 19, 2021, 5:33pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/6 "2021-12-19T17:33:13Z")

</div>

But depending on your actual problem, this may not be the best solution. without knowing this, @stevengj is probably right in saying, that a better data structure would lead to a better solution.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 19, 2021, 5:39pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/7 "2021-12-19T17:39:39Z")

</div>

I’ll try to design my system using `Dict` instead, but it is always nice to learn of a new trick.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 19, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/8 "2021-12-19T18:03:35Z")

</div>

I’d say most of the time if you have to resort to `eval` you’re doing it wrong and should step back to reconsider.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 19, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/9 "2021-12-19T18:03:48Z")

</div>

> [@oheil](#):
>
> But depending on your actual problem, this may not be the best solution. without knowing this,

I’ll go out on a limb and say that in \> 99% of cases, using `eval` with global variables is not the best solution.

> The goal of my if loop is to assign the respective matrix to Z.

This is not what I meant by “more context”. I wanted you to explain why you think you need to so something like this.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 19, 2021, 6:10pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/10 "2021-12-19T18:10:06Z")

</div>

Maybe something like this is what sevengj meant, which quite clean and avoids the eval:

```julia
julia> A = rand(10,10); B = rand(10,10); C = rand(10,10); D = rand(10,10);

julia> vars = Dict("A" => A, "B" => B, "C" => C, "D" => D)
Dict{String, Matrix{Float64}} with 4 entries:
  "B" => [0.504199 0.170964 … 0.367923 0.658817; 0.881139 0.116055 … 0.178827 0.624661; … ; 0.296164 0.0850091 … 0.142666 0.651318; 0.…
  "A" => [0.809202 0.513267 … 0.487085 0.138561; 0.366652 0.379529 … 0.104835 0.0992703; … ; 0.439847 0.570735 … 0.644324 0.200732; 0.…
  "C" => [0.884361 0.078709 … 0.944589 0.268905; 0.888668 0.723977 … 0.957192 0.61467; … ; 0.473355 0.823675 … 0.0485727 0.312304; 0.1…
  "D" => [0.153201 0.970533 … 0.768051 0.652922; 0.219684 0.135139 … 0.193671 0.335816; … ; 0.473319 0.460035 … 0.746215 0.807457; 0.9…

julia> Z = vars["A"]
10×10 Matrix{Float64}:
 0.809202 0.513267 0.26274 0.57556 0.0991993 0.17318 0.975175 0.226455 0.487085 0.138561
 0.366652 0.379529 0.908708 0.936167 0.771327 0.976005 0.582927 0.928687 0.104835 0.0992703
 0.393059 0.669549 0.606634 0.113105 0.456589 0.875129 0.787877 0.397731 0.571865 0.554807
 0.561105 0.638307 0.537919 0.800292 0.442311 0.114075 0.717245 0.631823 0.404449 0.590554
 0.953813 0.957735 0.0580233 0.0492704 0.176854 0.20227 0.991575 0.0915628 0.761263 0.941246
 0.361479 0.657399 0.182579 0.281445 0.638095 0.751553 0.306348 0.184897 0.32337 0.912145
 0.715396 0.324467 0.671328 0.826675 0.368116 0.68645 0.670547 0.70813 0.959706 0.0967608
 0.1752 0.199804 0.252097 0.850838 0.269474 0.861059 0.70255 0.968217 0.953311 0.694385
 0.439847 0.570735 0.447643 0.38973 0.431662 0.486043 0.712614 0.232222 0.644324 0.200732
 0.53924 0.387468 0.58615 0.397755 0.292034 0.216963 0.932639 0.248254 0.137529 0.744861

```

(be careful in all these cases: do you want `Z` to be the exact same array, or a copy of it?)

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 19, 2021, 6:32pm UTC](https://discourse.julialang.org/t/how-to-simply-this-without-writing-a-loop/73337/11 "2021-12-19T18:32:06Z")

</div>

Thanks! Yes, that’s what I need.

To answer Steven’s question, my use case is pretty simply. I just need to get the Z values depending on the variable name and then make a map plot. By storing my variables in a Dictionary, I can call them out based on the variable name (as a key) easily.
