1

I have a problem with elseif, which is used in Lua function. I am using LuaEdit 2010 on Windows and in case of first code, the program freezes. The second one works but is pretty ugly and incase of more elseifs also pretty unusable. I think it should be working in both cases but its not and I have no idea why, please help me. Lua Edit seems like the function is not closed. This code is not working

function read_this()
    char=read_char()
    word=""
    if char=="~" then    word = word..char
                        char=read_char()
                        if char == "+" then      
                               formating=true 
                               word=word..char
                        elseif char == "-" then 
                               formating=false
                               word = word..char 
                        else word = word..char
                        end
                    write(word,file2)
    else    print("something what is not problem")
    end                 
end

This code is working for me.

function read_this()
    char=read_char()
    word=""
    if char=="~" then    word = word..char
                        char=read_char()
                        if char == "+" or char == "-" then  
                               if char == "+" then formating=true end
                               if char == "-" then formating=false end
                               word = word..char 
                        else word = word..char
                        end
                    write(word,file2)
    else    print("something what is not problem")
    end                 
end
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Hrablicky
  • 143
  • 1
  • 9

1 Answers1

4

Both examples look functionally the same to me. I would really work on your use of newlines, spaces and indentation because it looks really messy the way you have it. Your first example I would write as

function read_this()
    char = read_char()
    word = ""
    if char == "~" then
        word = word .. char
        char = read_char()
        if char == "+" then      
            formating = true 
            word = word .. char
        elseif char == "-" then 
            formating = false
            word = word .. char 
        else
            word = word .. char
        end
        write(word,file2)
    else
        print("something what is not problem")
    end                 
end

What I also notice is that in every case you do a word = word .. char anyway so no need to put it in every if statement and put it after it like this:

function read_this()
    char = read_char()
    word = ""
    if char == "~" then
        word = word .. char
        char = read_char()
        if char == "+" then      
            formating = true 
        elseif char == "-" then 
            formating = false 
        end
        word = word .. char
        write(word,file2)
    else
        print("something what is not problem")
    end                 
end
Ivo
  • 18,659
  • 2
  • 23
  • 35
  • 2
    Beat me to the same conclusion - I was just about to hit send when I saw "1 new answer". :p – Josh Mar 12 '15 at 10:28