# Wondering Why the Variable is Not Used

**URL:** <https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 6, 2021, 3:51am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731 "2021-09-06T03:51:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 6, 2021, 3:51am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/1 "2021-09-06T03:51:24Z")

</div>

![截屏2021-09-06 上午11.49.39](https://global.discourse-cdn.com/julialang/original/3X/7/b/7bc8b94d6e3c8f5c3e8eeb13e1d3ec4d174eddab.png)  
I wonder why the grey variable names exist. As I concerned, I think it should have been used.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 6, 2021, 6:12am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/2 "2021-09-06T06:12:58Z")

</div>

Using `meta.parse` and `eval` for variable names means your editor can’t know you’re accessing the variables. It’s also a big code smell, much better to stay inside Julia if possible.

(Also, can I interest you in `Unitful`, so you don’t have to check if the use supplies Ångström or not?)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 6, 2021, 11:18am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/3 "2021-09-06T11:18:34Z")

</div>

> [@Edward\_Kong](#):
>
> I wonder why

Welcome to discourse and wonder no more by reading [the guidelines](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) to better post questions here.

_NB: and hi to you too._

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 8, 2021, 5:07am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/4 "2021-09-08T05:07:36Z")

</div>

Thanks for your reply. I would like to use Unitful to customize the unit convertion part in my program. However, it seems even I remove the mate programming part. This error seems to be still there.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 8, 2021, 5:44am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/5 "2021-09-08T05:44:02Z")

</div>

What editor are you using? And could you maybe show your current code (or ideally a shortened version)?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [September 8, 2021, 8:05am UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/6 "2021-09-08T08:05:57Z")

</div>

Look’s like a bug. Can you share your code in a copyable form?

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 12:08pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/7 "2021-09-10T12:08:08Z")

</div>

Sorry for my late reply. I have tried some ways to solve this problem but the outcome turns out to be very baffling.

I simplify the meta programming part as follow:

```julia
using Printf
real_a = [2.7605462334516533, 0.0, 0.0]
real_b = [5.0000000000000000, 0.0, 0.0]

# true
@printf "Real Space Axis %s: %20.10f %20.10f %20.10f\n" string(:real_a)[end] eval(:real_a)[1] eval(:real_a)[2] eval(:real_a)[3] 

# true
n = :real_a
@printf "Real Space Axis %s: %20.10f %20.10f %20.10f\n" string(n)[end] eval(n)[1] eval(n)[2] eval(n)[3] 

for n in (:real_a, :real_b)
    @printf "Real Space Axis %s: %20.10f %20.10f %20.10f\n" string(n)[end] eval(n)[1] eval(n)[2] eval(n)[3] 
end

```

and it works well.

This makes me think that maybe it’s not the meta programming part’s problem. Because at the former part , the situation has already gotten strange.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/9/79e202e81304ee726cbed066a207f1ae61f87169.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/06a73056c30b7b8b765ef85e58939d633ce886b4.png)

It seems that I have done something not lawful since I open the input file. I can’t figure it out.  
The code is attached.

```julia
function In( InFile :: String = "adphonon.in" )
    @printf "\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~INPUT INFORMATION~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n\n"
    @warn "Input Must be Carefully Checked. Our Journey Begins Here."
    tags = keys(InTags)
    open(InFile) do f
        keys = []; values = []
        lines = strip.(readlines(f))
        lines = [line for line in lines if !startswith(line, "#") && length(line)!=0]
        for line in lines
            ls = split(line)
            temptag = ls[1]; tempvalue = ls[3]
            if temptag ∈ tags
                typ = InTags[temptag]
                try
                    tempvalue = Convert(typ, tempvalue)
                catch
                    exit()
                end
                push!(keys, temptag)
                push!(values, tempvalue)
            end
        end
        return Dict(zip(keys,values))
    end
end

```

```julia
function StructureIn( InFile :: String = "structure.in" )
    open(InFile) do f
        lines = [x for x in strip.(readlines(f)) if length(x) != 0]
        
        if lines[1] ∉ ("Ang", "A") 
            error("Line 1 is not lawful\nJob Stops Here.")
            exit()
        else
            RealUnit = "Angstrom"
        end
        real_a = parse.(Float64,split(lines[2]))
        
        real_b = parse.(Float64,split(lines[3]))
        real_c = parse.(Float64,split(lines[4]))
        @printf 
        # println(real_a)
        # # @printf "Real Space Axis %s: %20.10f %20.10f %20.10f\n" string(:real_a)[end] eval(:real_a)[1] eval(:real_a)[2] eval(:real_a)[3] 
        # for n in (:real_a, :real_b, :real_c)
        # println(eval(n))
        # @printf "Real Space Axis %s: %20.10f %20.10f %20.10f\n" string(n)[end] eval(n)[1] eval(n)[2] eval(n)[3] 
        # end
        @info "Generating Reciporcal Space Axis ..."
        recip_a = 2 * π * LinearAlgebra.cross(real_b,real_c) / (dot(real_a, LinearAlgebra.cross(real_b,real_c)))
        recip_b = 2 * π * LinearAlgebra.cross(real_c,real_a) / (dot(real_b, LinearAlgebra.cross(real_c,real_a)))
        recip_c = 2 * π * LinearAlgebra.cross(real_a,real_b) / (dot(real_c, LinearAlgebra.cross(real_a,real_b)))  
        # for n in ["recip_a", "recip_b", "recip_c"]
        # name = Meta.parse(n)
        # @printf "Reciporcal Space Axis %s: %20.10f %20.10f %20.10f\n" n[end] eval(name)[1] eval(name)[2] eval(name)[3] 
        # end
        @info "Getting Atoms' Positions"
        positions = []; species = []; numbers = []
        @printf "%2s %3s %15s %20s %20s\n" "ele" "ind" "a" "b" "c"
        for (ii,line) in enumerate(lines[6:end])
            specie = split(line)[1]; push!(species, specie)
            position = parse.(Float64, split(line)[2:end])
            @printf "%2s %3d %20.10f %20.10f %20.10f\n" specie ii position[1] position[2] position[3]
            push!(positions, position)
        end
        unispecies = unique(species)
        for spe in species
            push!(numbers, findfirst(x->x==spe, unispecies))
        end
        return RealUnit, real_a, real_b, real_c, recip_a, recip_b, recip_c, numbers, positions
    end
end

```

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 12:09pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/8 "2021-09-10T12:09:06Z")

</div>

The code has been attached. Thanks!

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 12:11pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/9 "2021-09-10T12:11:29Z")

</div>

Thanks for your response again and sorry for my slow reply. I’m from China, so it’s not so convenient to connect this forum.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [September 10, 2021, 3:21pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/10 "2021-09-10T15:21:15Z")

</div>

Do you get an error, is it running poorly or is it a formatting thing? I don’t know what editor you’re using, and it’s definitely not the one I use, so if it’s a problem with the formatting I’m not sure I can help, at least not without a description of which part of it is wrong and what you’re expecting.

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 3:33pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/11 "2021-09-10T15:33:37Z")

</div>

Actually, the julia says the variable is not defined.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 10, 2021, 3:43pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/12 "2021-09-10T15:43:58Z")

</div>

Why

> [@Edward\_Kong](#):
>
> `eval(:real_a)[1]`

instead of simply

```julia
real_a[1]

```

?

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 3:53pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/13 "2021-09-10T15:53:46Z")

</div>

I have three variables like real\_a, real\_b and real\_c. So I try on writting it in a “for” loop using metaprogramming.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 10, 2021, 3:56pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/14 "2021-09-10T15:56:50Z")

</div>

OK, but it still seems like the hard way of doing it. Beware that eval works in the global scope and you may get unintended side effects.

---

<div class="post-metadata">

**Author:** ![Edward\_Kong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edward_kong/32/28904_2.png) [@Edward\_Kong](https://discourse.julialang.org/u/Edward_Kong)\
**Post date:** [September 10, 2021, 4:01pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/15 "2021-09-10T16:01:06Z")

</div>

Oh, I think that is the root of my problem. Thanks for your advice.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 10, 2021, 4:08pm UTC](https://discourse.julialang.org/t/wondering-why-the-variable-is-not-used/67731/16 "2021-09-10T16:08:01Z")

</div>

Using a named tuple is probably a better approach.
