Weird error in for loop

Hello everybody!
This is the program:

f = open("C-V.ini")
readString = read(f,String)
word = split(readString,"\n")


buffer = ""

addLine = false

progStruc = []

for line in word
	if occursin("#NAME:",line)
		addLine = true
	end
	println(addLine)
end

and this is the C-V.ini file:

#NAME:PAUSE
	PARAMETERS:delay
	pause:delay
#END

and the error is

ERROR: LoadError: UndefVarError: **addLine not defined**
Stacktrace:
 [1] top-level scope at C:\Users\Masurari\Desktop\program julia\v1.jl:18
 [2] include(::Module, ::String) at .\Base.jl:377
 [3] exec_options(::Base.JLOptions) at .\client.jl:288
 [4] _start() at .\client.jl:484
in expression starting at C:\Users\Masurari\Desktop\program julia\v1.jl:12

Now if i romove the following line:

println(addLine)

the program run verry well.
Something similar is happening if I run this code:

f = open("C-V.ini")
readString = read(f,String)
word = split(readString,"\n")


buffer = ""

addLine = false

progStruc = []

for line in word
	if occursin("#NAME:",line)
		addLine = true
	end
	if addLine == true
		buffer = buffer * line
	end
end

give me that

ERROR: LoadError: UndefVarError: **buffer not defined**
Stacktrace:
 [1] top-level scope at C:\Users\Masurari\Desktop\program julia\v1.jl:13
 [2] include(::Module, ::String) at .\Base.jl:377
 [3] exec_options(::Base.JLOptions) at .\client.jl:288
 [4] _start() at .\client.jl:484
in expression starting at C:\Users\Masurari\Desktop\program julia\v1.jl:12

this time buffer is not defined
I have no idea why this is happen!
Thank you in advance!!!

Ok !!! I found the answer!
https://github.com/JuliaLang/julia/issues/28523
It works if declare the variable globally or if you put the entire code in let…end block.
Now works. No offense, but this programming language is a joke! In which other programming language you must declare a variable as globally if you want to be recognized inside a for loop ?
I am very disappointed !
The Julia programming language should be an advance in this field not another garage programming language with obscure error which eat me a day to figure out!

Instead of using globals, you can also put your main code inside a function like this

function dostuff(word)
    buffer = ""

    addLine = false

    progStruc = []

    for line in word
        if occursin("#NAME:",line)
            addLine = true
        end
        println(addLine)
    end
end

word = ["#NAME:", "otherString"]
dostuff(word)

This will run without errors and give the output

true
true
1 Like

Thank you @mlanghinrichs!

I understand your frustration, but you may be overreacting a bit (as much as this particular issue infuriates me). That said, you will never run into this exact issue if you do the following:

  • Use jupyter as your interactive terminal - which patches this error when doing interactive work. Furthermore, in Julia 1.5 it will be the same thing in the REPL.
  • Wrap loops/etc. in funcitons in your .jl files. You will usually want to do this for performance reasons anyways, and you can copy/paste code between your functions/jupyter/ and (in 1.5) the REPL.

If you follow this simple advice, you will never, ever type global again. If you do, then you are probably have a design flaw in your code, or have become an expert and know what you are doing.

I also find this behaviour very annoying, as it makes prototyping functions much more difficult (you can’t just add/remove the function wrapping and call it a day), and I really don’t care about leaving a few extra variables lying around (if I’m doing it in the REPL then it’s probably useful stuff for debugging).

But this really is the only major issue I have with Julia, and it’s otherwise very nice to work with. I certainly wouldn’t describe it as “garbage”.

It might have been better to ask about the behavior rather than just venting your spleen. This behavior was introduced recently when inconsistencies where found in regards to if statements and loops. I believe the decision to work this way was because most people using Julia are interested in speed which means no global’s and putting everything in functions. Therefor making global’s “smell”, to use their terminology, was thought to be a good idea.

However the reaction for new users has been problematic, including explaining to new users what is happening. So after much bitching and moaning, and not everyone is truly happy with the decision, it was decided in the next release to “rollback” the behavior to something new users won’t trip over constantly.

I also feel that trashing a whole language because of one weird quirk is a bit harsh. Otherwise I would say English should be discarded because this is valid: " All the faith he had had had had no effect on the outcome of his life."

1 Like

Well, both Apple and Google started up in a garage, and look where they are today.

This, however, is unnecessary, imho:

2 Likes

Sorry about the harsh words above. I am a researcher and I am not a begginer in programing. I have a 2000 lines python program that control 9 devices for data acquisitions using GPIB and serial protocol. The python program is painfully slow so I discovered the Julia language which is very fast but with some weird issues which driving me nuts. The problem is not that i have to use global variable, the problem is that I had to dig until I find an answer that should be in documentation. Sorry again and thank you for your answers.

OP I think the best solution out of all the ones mentioned is to use let. Want to increment a variable? Then do

total = let s = 0
    for i in 1:10
        s += i
    end
    return s
end

Which documentation?

This? Or maybe you meant this? Or perhaps this or this.

Or maybe you missed the previous conversations here on Discourse. Here’s one.

1 Like