Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
can I simplify this for loop? — Gideros Forum

can I simplify this for loop?

MoKaLuxMoKaLux Member
edited September 2019 in General questions
I have a question please. Can I simplify this for loop?
	local map = {}
	for line in io.lines("map1.txt") do
		map[#map + 1] = line
	end
 
	local mapid = {}
	for r = 1, #map do
		mapid[r] = {}
		for c = 1, #map[r] do
			mapid[r][#mapid[r] + 1] = "x"
		end
	end
I try to use only one table instead of two. Is this possible? Thank you for your time.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • talistalis Guru
    edited September 2019 Accepted Answer
    I am not an expert in Lua code optimizations but found a good article, and start to read when i saw your post. So many good suggestions exist, have a look. Maybe it will not help in this specific question but in general you will gain favor of it.
    https://www.lua.org/gems/sample.pdf

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited September 2019
    thank you very much @talis
    Rule #1:Don’t do it.
    Rule #2:Don’t do it yet.(for experts only)
    I am trying to optimize my code but sometimes I complicate it more!
    For my problem I simply do:
    	local map = {}
    	for line in io.lines("map1.txt") do
    		map[#map + 1] = line
    	end
     
    	for r = 1, #map do
    		for c = 1, #map[r] do
    			local id = string.sub(map[r], c, c + x - 1)
    		end
    	end
    Once again thank you!
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Apollo14Apollo14 Member
    edited September 2019 Accepted Answer
    If we talk about optimization-masturbation, it's bad practice to use '#' (as well as 'table.insert', cause this way lua recalculates array size every time).
    --"Bad":
    local map = {}
    for line in io.lines("map1.txt") do
    	map[#map + 1] = line
    end
     
    --"Good":
    local map = {}
    local mapArrSize=1
    for line in io.lines("map1.txt") do
    	map[mapArrSize] = line
    	mapArrSize+=1
    end
    I'm too sleepy now to suggest anything wise :D

    Likes: MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
  • thank you @Apollo14 I will change my code to reflect your optimization.
    BTW: my initial purpose was not to optimize but to simplify my loops because it seemed to me that those two loops could be merged into one, but I was wrong. I still have a lot to learn regarding lua and gideros.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • Apollo14Apollo14 Member
    edited September 2019
    Take a pil every day! :smiley:
    https://www.lua.org/pil/contents.html

    (I stopped taking them somewhere in the middle :D but I feel like I should continue my treatment)

    Likes: MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.