# How to use string substitution in variable names

**URL:** <https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192>\
**Category:** New to Julia\
**Tags:** strings\
**Created:** [February 13, 2021, 5:57am UTC](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192 "2021-02-13T05:57:30Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 13, 2021, 7:32pm UTC](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192/11 "2021-02-13T19:32:20Z")

</div>

While I agree that this is perhaps not an appropriate place for using meta-programming, I beleive it’s still useful to also answer the question that was asked. Since the only answer here that attempts to address the actual question asked uses `eval`, I want to note that something like this can be done very cleanly without `eval`, and is actually an important part of julia’s Cartesian indexing implementation.

The way to do this when the number of unit vectors needed is known at macroexpansion time is to write a macro. Here’s one possible way:

```julia
julia> macro define_basis_vecs(sym::Symbol, n::Integer)
           Expr(:block, __source__ , map(i -> :($(esc(Symbol(sym, i))) = $( [j==i for j in 1:n] )), 1:n)...)
       end
@define_basis_vecs (macro with 1 method)

```

We can macroexpand it to make sure it generates the right code:

```julia
julia> @macroexpand @define_basis_vecs e_ 5
quote
    #= REPL[15]:1 =#
    e_1 = Bool[1, 0, 0, 0, 0]
    e_2 = Bool[0, 1, 0, 0, 0]
    e_3 = Bool[0, 0, 1, 0, 0]
    e_4 = Bool[0, 0, 0, 1, 0]
    e_5 = Bool[0, 0, 0, 0, 1]
end

```

Unlike solutions with `eval`, this will work in the local scope.

```julia
julia> let 
           @define_basis_vecs e_ 7
           e_1 * e_7'
       end
7×7 BitMatrix:
 0 0 0 0 0 0 1
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0

```

and won’t leak any variables out of that scope:

```julia
julia> e_1
ERROR: UndefVarError: e_1 not defined

```

But again, as was pointed out earlier, this is a usecase that is usually better satisfied in much more ‘boring’ ways without any metaprogramming.

If you’ve spoken to your doctor and you think that Metaprogramming may be right for you, I’d recommend reading this section of the manual thoroughly: [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192)._
