# Access Variable (or Variable Values) using String Variable Name

**URL:** <https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905>\
**Category:** New to Julia\
**Created:** [December 16, 2020, 1:21am UTC](https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905 "2020-12-16T01:21:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Shupe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_shupe/32/11298_2.png) [@Steve\_Shupe](https://discourse.julialang.org/u/Steve_Shupe)\
**Post date:** [December 16, 2020, 1:21am UTC](https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905/1 "2020-12-16T01:21:55Z")

</div>

I’m working through the Advent of Code problems. One of them involves keeping track of directional moves, given as strings (“N”, “S”, “E”, “W”) and integer values. I’ve set up variables N, S, E, and W to track movement. Passing on whether this is a sensible way to solve this part of the problem, I’d like to be able to use the string values to access the same-named variables. For example, if I have (“N”, 30) as input, I can add 30 to the value of N without having to write out `if x == "N" N += 30` for every directional string letter.

I’ve tried using a Dictionary and Symbols, but I suspect this involves a metaprogramming/macro solution. I researched how to to accomplish this online, and read the Julia “metaprogramming” docs, but the solution isn’t coming to me. Any help would be appreciated. I know this is something that Julia ought to be able to do.

Thank you!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 16, 2020, 1:28am UTC](https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905/2 "2020-12-16T01:28:15Z")

</div>

I think a `NamedTuple` or `Enum`would be what you want here. No meta-programming necessary

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 16, 2020, 6:20am UTC](https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905/3 "2020-12-16T06:20:53Z")

</div>

You might want to use symbols `:N`, `:S` etc, instead of strings.  
A dictionary then seems like a perfectly good (although probably not optimally fast) solution.

---

<div class="post-metadata">

**Author:** ![Steve\_Shupe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steve_shupe/32/11298_2.png) [@Steve\_Shupe](https://discourse.julialang.org/u/Steve_Shupe)\
**Post date:** [December 16, 2020, 6:54pm UTC](https://discourse.julialang.org/t/access-variable-or-variable-values-using-string-variable-name/51905/4 "2020-12-16T18:54:39Z")

</div>

Thanks both of you, I realize I was over-complicating the problem. The solution (a regular old-fashioned Dict) was so simple that I couldn’t see it. Slow progress …
