• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

How do i overwrite an variable inside an if loop (1 Viewer)

bluedxca93

Newcomer
Joined
Mar 13, 2021
Messages
2
Reaction score
0
Am thinking of a script to autocrop small pngs using lua 5.1 and imlib2 on ubuntu.
while doing this i did get the idea of writing the following code:
Code:
  for y=0, xhalf do
     c1 = im:get_pixel(xmid, y)
          c1a = (c1.alpha)
          if c1a < 20 then temp_top=y  io.stdout:write( "top_margin=",y+1, "\n")  end
  end
What i want there is that temp_top is a variable in lua and that it only is defined for the biggest temp_top possible inside that for loop and exported to a variable called top_margin that i can use later.
inside scripts,zip i did the following hackish workaround:
lua script.lua > offset-analyz; cat offset-analyz | sort -r | grep -m 1 top;.
This does export top_margin in a way i can use it in bash. That works but there schould be a way to do it entirely in lua.
 

Attachments

  • scripts.zip
    987 bytes · Views: 0

GavinW

Newcomer
Creator of RiscLua
Joined
Oct 21, 2020
Messages
40
Reaction score
17
Age
82
Location
UK
Website
www.wra1th.plus.com
What about this?
Lua:
function (xmid, im)
 local top_margin = 1
 for y = 0, xhalf do
     local c1 = im.get_pixel (xmid, y)
     if c1.alpha < 20 then top_margin = y + 1 end -- if
 end -- for 
 return top_margin
end -- function
 
Last edited:
Top