# Very mysterious error message

**URL:** <https://discourse.julialang.org/t/very-mysterious-error-message/35276>\
**Category:** Tooling\
**Tags:** juliapro\
**Created:** [February 28, 2020, 7:40am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276 "2020-02-28T07:40:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 7:40am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/1 "2020-02-28T07:40:38Z")

</div>

# The following code

# file=ARGS[1] lines=readlines(file) lenFile=length(lines) l3=lines[3] l4=lines[4] l5=lines[5] l6=lines[6] sl3=split(l3) NA=parse(Int64,sl3[1]) sl4=split(l4) nx=parse(Int64,sl4[1]) sl5=split(l5) ny=parse(Int64,sl5[1]) sl6=split(l6) nz=parse(Int64,sl6[1]) npt=nx_ny_nz println(nx,ny,nz) println(NA," ",npt) istart=6+NA+1 vec=zeros(Float64,npt) q=1 for i in istart:lenFile println(q) tmp=split(lines[i]) ll=length(tmp) for j in 1:ll vec[q+j-1]=parse(Float64,tmp[j]) end q=q+ll end

# with the following file as argument

# Cube file generated by NWChem DENSITY 5 -6.000000 -6.000000 -6.000000 201 0.060000 0.000000 0.000000 201 0.000000 0.060000 0.000000 201 0.000000 0.000000 0.060000 6 6.000000 0.000000 0.000000 0.000000 1 1.000000 -1.185992 -1.185992 -1.185992 1 1.000000 1.185992 1.185992 -1.185992 1 1.000000 -1.185992 1.185992 1.185992 1 1.000000 1.185992 -1.185992 1.185992 0.38635E-08 0.42174E-08 0.45990E-08 0.50101E-08 0.54523E-08 0.59275E-08 0.64376E-08 0.69844E-08 0.75700E-08 0.81963E-08 0.88654E-08 0.95794E-08 0.10340E-07 0.11150E-07 0.12012E-07 0.12926E-07 0.13896E-07 0.14924E-07 0.16011E-07 0.17160E-07 0.18373E-07 0.19652E-07 0.20998E-07 0.22413E-07 0.23900E-07 0.25459E-07 0.27092E-07 0.28801E-07 0.30586E-07 0.32449E-07 0.34389E-07 0.36409E-07 0.38508E-07 0.40686E-07 0.42943E-07 0.45279E-07 0.47692E-07 0.50183E-07 0.52750E-07 0.55391E-07 0.58104E-07 0.60887E-07 0.63737E-07 0.66652E-07 0.69628E-07 0.72662E-07 0.75748E-07 0.78884E-07 0.82064E-07 0.85284E-07 0.88537E-07 0.91818E-07 0.95122E-07 0.98441E-07

# produces the output

# ERROR: LoadError: UndefVarError: q not defined Stacktrace: [1] top-level scope at /home/braunm/cubeReader/readCube.jl:23 [2] include at ./boot.jl:328 [inlined] [3] include\_relative(::Module, ::String) at ./loading.jl:1105 [4] include(::Module, ::String) at ./Base.jl:31 [5] exec\_options(::Base.JLOptions) at ./client.jl:287 [6] \_start() at ./client.jl:460 in expression starting at /home/braunm/cubeReader/readCube.jl:22 201201201 5 8120601

It is very strange, since q has been defined by q=1

It would have been much easier to report this  
If I knew how to attach files!

Regards

Moritz

Regards  
Moritz

---

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [February 28, 2020, 7:58am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/2 "2020-02-28T07:58:14Z")

</div>

Welcome Moritz,

first of all: Your code in bold font is quite hard to read. I suggest that you use imbed your code in ``` (at the beginning and at the end).

Regarding your problem: Up until Julia version 1.4.x a for loop will create an own scope in the REPL. So every variable that you change in the for loop, that has reference outside of the loop, must be marked as global:

```julia
for i in istart:lenFile
    global q
    println(q)
    tmp=split(lines[i])
    ll=length(tmp)
    for j in 1:ll
        vec[q+j-1]=parse(Float64,tmp[j])
    end
    q=q+ll
end

```

You can find more on this here:  
[https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1)

This behavior will change with Julia v1.5 (see: [RFC: bring back v0.6 scope rules in the REPL by JeffBezanson · Pull Request #33864 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33864))

---

<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:** [February 28, 2020, 7:59am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/3 "2020-02-28T07:59:52Z")

</div>

It looks like this is a scoping issue - see the docs [here](https://docs.julialang.org/en/v1/manual/variables-and-scoping/).

Try wrapping your code in a function (i.e. just add `function main()` at the top and `end` at the bottom) and run it by calling `main()`. Or annotate `q` as a global variable. Alternatively you can also use IJulia notebooks, which have a different approach to global variables in local scopes.

---

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 8:42am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/4 "2020-02-28T08:42:29Z")

</div>

Thanks

WIll the references to lines and vec work  
or do i have to also mark them global!  
What a whole lot of effort!  
What will be the behaviour in 1.5 ?

Regards

Moritz

---

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [February 28, 2020, 9:05am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/5 "2020-02-28T09:05:50Z")

</div>

Only the variables that you change have to be marked as global. Since your not changing the references to lines and vec, there is no need to mark them as global.

Note that this behavior only arises in the global scope (e.g. in the REPL). As soon as you wrap your for loop in a function or a let block or in a module, there is no need to set it global.  
In Julia version 1.5 you do not have to set it to global either. That means your code in the first place will work without modification.

---

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 9:22am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/6 "2020-02-28T09:22:23Z")

</div>

Thanks a lot!

I now know what do to!

Regards

Moritz

---

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 9:36am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/7 "2020-02-28T09:36:42Z")

</div>

A very simple question  
how do I calculate the dot product between tow vectors  
I could not find it by googling!

Regards

Moritz

---

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 9:39am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/8 "2020-02-28T09:39:27Z")

</div>

Hi  
I did find the definition '\* which is not exactly intuitive!

Regards

Moritz

---

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [February 28, 2020, 9:40am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/9 "2020-02-28T09:40:38Z")

</div>

That’s as simple as:

```julia
a = ones(4); b = ones(4);
a' * b

```

The symbol `'` means adjoint (or transpose for real valued variables)  
or:

```julia
using LinearAlgebra
dot(a, b)

```

---

<div class="post-metadata">

**Author:** ![moritz\_braun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moritz_braun/32/13087_2.png) [@moritz\_braun](https://discourse.julialang.org/u/moritz_braun)\
**Post date:** [February 28, 2020, 9:47am UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/10 "2020-02-28T09:47:17Z")

</div>

Thanks!  
This makes the “translation” from other languages easier

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 12, 2023, 4:41pm UTC](https://discourse.julialang.org/t/very-mysterious-error-message/35276/11 "2023-09-12T16:41:32Z")

</div>

Archived on September 12, 2023, 4:41pm UTC


