Very mysterious error message

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=nxnynz
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

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:

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

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)

1 Like

It looks like this is a scoping issue - see the docs here.

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.

1 Like

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

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.

Thanks a lot!

I now know what do to!

Regards

Moritz

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

Regards

Moritz

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

Regards

Moritz

That’s as simple as:

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

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

using LinearAlgebra
dot(a, b)

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