# Lua Programming Examples

## Get School Day Numbers

```months = {}

months[9] = {
9,
30,
{
3, 21
}
}
months[10] = {
10,
31,
{
8
}
}
months[11] = {
11,
30,
{
2
}
}
months[12] = {
12,
31,
{
7, 24, 25, 26, 27, 28, 31
}
}
months[13] = {
13,
31,
{
1, 2, 3, 4, 25
}
}
months[14] = {
14,
28,
{
18
}
}
months[15] = {
15,
31,
{
1, 11, 12, 13, 14, 15
}
}
months[16] = {
16,
30,
{
19, 22
}
}
months[17] = {
17,
31,
{
20
}
}
months[18] = {
18,
30,
{
7, 28, 29, 30
}
}

startY = 2018
startM = 9
startD = 4

endY = startY + 1
endM = 18
endD = 27

testY = 2018
testM = 9
testD = 19

currentY = testY
currentM = testM
currentD = testD

function calculateWeekdays()
local weekMap = {}

for i = startM, 18 do
weekMap[i] = {}

for j = 1, months[i][2] do
if (i == startM and j == 1) then
weekMap[startM][1] = 7
else
if (j == 1) then
weekMap[i][j] = weekMap[i-1][months[i-1][2]] + 1
else
weekMap[i][j] = weekMap[i][j-1] + 1
end
end

if ( weekMap[i][j] > 7 ) then
weekMap[i][j] = 1
end
end
end

print("Finished calculating Weekdays. Returning")

return weekMap
end

function calculateSchoolDays()
-- DayMap Value 9 == WeekEnd
-- DayMap Value 8 == FreeDay

local dayMap = {}

for i = startM, 18 do
dayMap[i] = {}

for j = 1, months[i][2] do
if (weekDays[i][j] == 1 or weekDays[i][j] == 7) then dayMap[i][j] = 9 -- Sunday (1) or Saturday (7)
elseif ( i == startM and j == startD ) then dayMap[i][startD] = 1 -- Start Day
elseif ( i == startM and j < startD ) then dayMap[i][j] = 0 -- Before Start Day
else
--print ("i: "..i.." | j: "..j)

--free = false

for f = 1, #months[ i ][ 3 ] do
if ( j == months[ i ][ 3 ][ f ] ) then
dayMap[i][j] = 8
f = #months[ i ][ 3 ]
free = true
print("Set true | i: "..i.." j: "..j)
goto later
end
end

if ( not free ) then
if ( j < 4 and i ~= startM ) then -- If the day of the month is less than 4 and the     month is not startM (The First month of the school year)
if ( j == 1 and dayMap[ i - 1 ][ months[ i - 1 ][ 2 ] ] == 9 ) then dayMap[i][j] = dayMap[ i - 1 ][ months[ i - 1 ][ 2 ] - 2 ] + 1 -- If the last day of the previous month is a weekend (9) then the current day is set to the last day of the previous month - (3 - j)'s day + 1
elseif ( j == 2 and dayMap[ i ][ j - 1] == 9) then dayMap[i][j] = dayMap[i - 1][     months[ i - 1 ][ 2 ] - 1 ] + 1
elseif ( j == 3 and dayMap[ i ][ j - 1] == 9) then dayMap[i][j] = dayMap[i - 1][     months[ i - 1 ][ 2 ] ] + 1
else
if ( j == 1) then dayMap[i][j] = dayMap[i - 1][months[i - 1][2]] + 1 -- If j ==     1 then the current day number is set to the last day of the previous month's day number + 1
else dayMap[i][j] = dayMap[i][j - 1] + 1 -- If j is NOT 1 then the current day     number is set to the previous day number + 1
end
end
elseif ( dayMap[i][j - 1] == 9 ) then
--print(i.." = i | j = "..j)
dayMap[i][j] = dayMap[i][j-3] + 1
--dayMap[i][j] = dayMap[i][j-3] + 1 - 10
else
--print(i.." = i | j = "..j)
dayMap[i][j] = dayMap[i][j - 1] + 1
end
else
print(">> Free Days <<")
print("---------------")
print("i: "..i.." j: "..j)

found = false
daysBack = 1
monthsBack = 0

repeat
if ( j <= daysBack ) then
daysBack = 0
monthsBack = monthsBack + 1
end

if (i == 13 and j == 7) then
dayMap[i][j] = 1

print("It is the first school day of January and the New Year, starting on Day 1")

found = true
elseif ( monthsBack > 0 ) then
if ( dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] ~= 8 and dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] ~= 9 ) then
dayMap[ i ][ j ] = dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] + 1

print("Months Back: "..monthsBack.." | Days Back: "..daysBack)

found = true
end
else
if ( dayMap[ i ][ j - daysBack ] ~= 8 and dayMap[ i ][ j - daysBack ] ~= 9 ) then
dayMap[ i ][ j ] = dayMap[ i ][ j - daysBack ] + 1

print("Months Back: "..monthsBack.." | Days Back: "..daysBack)

found = true
end
end

daysBack = daysBack + 1
until found

free = false
end

--if (i == 9 and j > 18 and j < 26) then goto later end
if ( dayMap[i][j] > 5 ) then dayMap[i][j] = 1 end

::later::
end
end
end

print("Finished calculating School Days. Returning")

return dayMap
end

weekDays = calculateWeekdays()

print("Just before Printing Weekdays")

--[[for i = startM, 18 do
print ("Month: "..i)
for j = 1, months[i][2] do
if weekDays[i][j] == 1 then     WEEKDAY = "Sunday 1"
elseif weekDays[i][j] == 2 then WEEKDAY = "Monday"
elseif weekDays[i][j] == 3 then WEEKDAY = "Tuesday"
elseif weekDays[i][j] == 4 then WEEKDAY = "Wednesday"
elseif weekDays[i][j] == 5 then WEEKDAY = "Thursday"
elseif weekDays[i][j] == 6 then WEEKDAY = "Friday"
elseif weekDays[i][j] == 7 then WEEKDAY = "Saturday 7"
else                            WEEKDAY = "an Unknown Weekday"
end

print("-- "..j.." is "..WEEKDAY)
end
end]]--

schoolDays = calculateSchoolDays()

print("Just before Printing School Days")

for i = startM, 18 do
print ("Month: "..i)
for j = 1, months[i][2] do
if weekDays[i][j] == 1 then         WEEKDAY = "Sunday 1"
elseif weekDays[i][j] == 2 then     WEEKDAY = "Monday"
elseif weekDays[i][j] == 3 then     WEEKDAY = "Tuesday"
elseif weekDays[i][j] == 4 then     WEEKDAY = "Wednesday"
elseif weekDays[i][j] == 5 then     WEEKDAY = "Thursday"
elseif weekDays[i][j] == 6 then     WEEKDAY = "Friday"
elseif weekDays[i][j] == 7 then     WEEKDAY = "Saturday 7"
else                                WEEKDAY = "an Unknown Weekday"
end

if schoolDays[i][j] == 0 then       SCHOOLDAY = "Before School Started"
elseif schoolDays[i][j] == 1 then   SCHOOLDAY = "Day 1"
elseif schoolDays[i][j] == 2 then   SCHOOLDAY = "Day 2"
elseif schoolDays[i][j] == 3 then   SCHOOLDAY = "Day 3"
elseif schoolDays[i][j] == 4 then   SCHOOLDAY = "Day 4"
elseif schoolDays[i][j] == 5 then   SCHOOLDAY = "Day 5"
elseif schoolDays[i][j] == 8 then   SCHOOLDAY = "a Free Day"
elseif schoolDays[i][j] == 9 then   SCHOOLDAY = "a Weekend Day"
else                                SCHOOLDAY = "an Unknown Day ("..schoolDays[i][j]..")"
end

print("-- "..j.." is "..WEEKDAY.." and is "..SCHOOLDAY)
end
end

function getSchoolDay(year, month, day)
if (year > startY) then
month = month + 12
end
end```

## Cachopoly_Decrypt

```function _Mzr(str)
local res = ""
local dtable = ";JH\021\n0/RMQ\016m&@,(Du8Csg\005\001\017I~9v\031\028\023b\f25x1.r[?\000eB'^\014-ho\020+qc3\022a\t\bl{\a<wt7O\\\030\018Y\024L)#y %XA4\019\rGS*zW!=k\002\015\v\006E}]\026V:\004FpdU`jZ>\003P\"\$T\029N\025n_\027K6i|f"
for i = 1, #str do
local b = str:byte(i)
if b > 0 and b <= 127 then
res = res .. string.char(dtable:byte(b))
else
res = res .. string.char(b)
end
end
return res
end

print(_Mzr("\017\018N2:\021BN,\021N\022,\02172:\127\127B\015N2,(X=}72,xNU=\0187\\A\018x\02172'@-\bo@-\boh\018(Nj},N-,(,72x\018x\022Nj,\021Nh}x:=N,(2:=\021BNj\018N\1273=\022,xj,NY,(B,f@-\bo@-\boQ[C\006@-\bo-[\028\$@-\bo\020[#\006@-\bo\017[R\$@-\bo@-\bo\017\018N\021\01872\021BN:xNj},\021,(NVB,==,Nj,xNV72=\018\021\021,=N\127\018(Nj:\021Nh}x:=,'N"))```

## Execute Lua Online

```local hook = require 'lib.samp.events'
local autocap = 1
local ka = 0
local spam = 1

function main()
repeat wait(100) until isSampAvailable()
sampRegisterChatCommand("reset", cmd_lastt)
end
function hook.onServerMessage(color,text)
if string.find(text, "%[%A+PAYDAY%A+%]") then
sampSendChat("/mafbizz")
elseif string.find(text, "saka uzbrukt bandas zonai") then
banda1, banda2 = string.match(text, "%[%a+%]%s(%a+)%s(%a+)%s")
sampAddChatMessage(string.format("%s | Iespamoja: %s %s ]", (os.date("[Teras sakums: {78a1c9}%H:%M:%S{FFFFFF}")), banda1, banda2), -1)
elseif string.find(text, "uzvar zonas iegushanu\$") then
if string.find(text, "(%[F%] Banda)") then
caca = os.date("Ieprieksejas teras beigas: %H:%M:%S")
file:write(caca)
file:close()
end
end
end

```

## asfd

```local lib = {};
-- lib.distance_method = Distance_method.great_circle;
-- 35km discount airports: CYBR,CYCG,CYCL,CYYG,CCH4,CYYQ,CZUM,CYXC,CYVZ,CYDF,CYEG,CYMM,CZFM,CYER,CYSM,CYXJ,CYFC,CYQX,CYQU,CYHM,CYOJ,CYGR,CYEV,CYFB,CYKA,CYLW,CYQK,CYGK,CYKF,CYVP,CYGW,CYGL,CYVC,CYQL,CYLL,CYXU,CYXH,CYQM,CYYY,CYVQ,CYQW,CYYB,CKQ3,CYPE,CYYF,CYZT,CYPD,CCP4,CYPA,CYXS,CYPR,CYRT,CYQF,CYUY,CYAM,CYZV,CYYD,CYS,CYXT,CYTH,CYTS,CYWK,CYZU,CYXY,CCA6,CYWL,CYZF
-- 65km discount airports: CYAV,CYXX,CYBW,CYYC,CYBL,CYRC,CYEG,CYND,CYHZ,CYNJ,CYMX,CYUL,CYHU,CYCD,CYOW,CYPK,CYQB,CYQR,CYXE,CYJN,CYQT,CYTZ,CYKZ,CYYZ,CZBB,CYVR,CYWH,CYYJ,CYWG

function findIcao(list, icao)
for i, v in ipairs(list) do
if (v == icao) then
return true
end
end
return false
end

function lib:calculate(flight_context, route_context, distance)
return self:estimate(flight_context, distance);
end

function lib:estimate(flight_context, distance)
local reducedDist = 0

local airportsWith35Discount = {'CYBR', 'CYCG', 'CYCL', 'CYYG', 'CCH4', 'CYYQ', 'CZUM', 'CYXC', 'CYVZ', 'CYDF', 'CYEG', 'CYMM', 'CZFM', 'CYER', 'CYSM', 'CYXJ', 'CYFC', 'CYQX', 'CYQU', 'CYHM', 'CYOJ', 'CYGR', 'CYEV', 'CYFB', 'CYKA', 'CYLW', 'CYQK', 'CYGK', 'CYKF', 'CYVP', 'CYGW', 'CYGL', 'CYVC', 'CYQL', 'CYLL', 'CYXU', 'CYXH', 'CYQM', 'CYYY', 'CYVQ', 'CYQW', 'CYYB', 'CKQ3', 'CYPE', 'CYYF', 'CYZT', 'CYPD', 'CCP4', 'CYPA', 'CYXS', 'CYPR', 'CYRT', 'CYQF', 'CYUY', 'CYAM', 'CYZV', 'CYYD', 'CYS', 'CYXT', 'CYTH', 'CYTS', 'CYWK', 'CYZU', 'CYXY', 'CCA6', 'CYWL', 'CYZF'}
local airportsWith65Discount = {'CYAV', 'CYXX', 'CYBW', 'CYYC', 'CYBL', 'CYRC', 'CYEG', 'CYND', 'CYHZ', 'CYNJ', 'CYMX', 'CYUL', 'CYHU', 'CYCD', 'CYOW', 'CYPK', 'CYQB', 'CYQR', 'CYXE', 'CYJN', 'CYQT', 'CYTZ', 'CYKZ', 'CYYZ', 'CZBB', 'CYVR', 'CYWH', 'CYYJ', 'CYWG'}

if (findIcao(airportsWith35Discount, flight_context.origin_icao_code) and
findIcao(airportsWith35Discount, flight_context.destination_icao_code)) then
reducedDist = 70000 -- departure and arrival discount 35km
elseif (findIcao(airportsWith35Discount, flight_context.origin_icao_code) or
findIcao(airportsWith35Discount, flight_context.destination_icao_code)) then
reducedDist = 35000 -- departure or arrival discount 35km
elseif (findIcao(airportsWith65Discount, flight_context.origin_icao_code) and
findIcao(airportsWith65Discount, flight_context.destination_icao_code)) then
reducedDist = 130000 -- departure and arrival discount 65km
elseif (findIcao(airportsWith65Discount, flight_context.origin_icao_code) or
findIcao(airportsWith65Discount, flight_context.destination_icao_code)) then
reducedDist = 65000 -- departure or arrival discount 65km
elseif (findIcao(airportsWith35Discount, flight_context.origin_icao_code) and
findIcao(airportsWith65Discount, flight_context.destination_icao_code)) then
reducedDist = 100000 -- departure or arrival discount 35km
elseif (findIcao(airportsWith65Discount, flight_context.origin_icao_code) and
findIcao(airportsWith35Discount, flight_context.destination_icao_code)) then
reducedDist = 100000 -- departure or arrival discount 35km
end

return math.max(distance-reducedDist,0)/1000 * self.unit_rate * math.sqrt(flight_context.mtom/1000)
end

return lib;```

## Execute Lua Online

```public class Hello {

public static void main(String[] args){
System.out.println("Hello Pluralsight")
}
}```

## local nao = "trai" if not nao == "gai" then print("nao eo phai gai") end

```local nao = "trai"
if not nao == "gai" then
print("nao eo phai gai")
end
```

## Execute Lua Online

```print(Asma)--الطباعه بدون علامات تنصيص تكون نتيجتها nil
x=9
y=7
print(x+y)
print(x*y)
print(x-y)
print(x/y)
if x>y then
print("good")
else
x=x+2   --  قيمة اكس الجديدة تساوي اكس القديمة + 2
end
print (x)```

## Execute Lua Online

```function toJson(obj)
local r = ''
local ia = false

for k, v in pairs(obj) do
if r ~= '' then
r = r .. ','
end

if type(k) == 'string' then
r = r .. utf8.char(0x0022)
r = r .. k
r = r .. utf8.char(0x0022) .. ':'
else
ia = true
end

if type(v) == 'table' then
r = r .. toJson(v)
elseif type(v) == 'string' then
r = r .. utf8.char(0x0022)
r = r .. v
r = r .. utf8.char(0x0022)
else
r = r .. v
end
end

if ia then
return '[' .. r .. ']'
else
return '{' .. r .. '}'
end
end

function func(l)
ml = 5
if l > ml or l < 0 then
l = ml
end

nl = l + 1

obj = {}
obj.maxLevel = ml

obj.speed = 5

obj.housingSpace = 1
obj.attackRange = 1

obj.hitPoints = 10 * l
obj.healthPoints = 100 * l

obj.buildTime =  100 * l

bc = {}
bc.gold = (28500 * math.pow (l, 2)) - (77500 * l) + 51000
obj.buildCost = bc

uc = {}
uc.gold = (28500 * math.pow (nl, 2)) - (77500 * nl) + 51000

obj.trainingTime =  10 * l

tc = {}
tc.gas = 10 * l
obj.trainingCost = tc

return obj
end

print (toJson(func(1)))```

## Execute Lua Online

```local thing = [[
-- Put script here
]]

local encoded = thing:gsub(".", function(bb) return "\\" .. bb:byte() end) or thing .. "\""

print(encoded)
print("Put the above encoded string into the loadstring below between the quotation marks for the obfuscated scrippts.")

## Execute Lua Online

```print("Hello World!")
print("hello world!")
io.write ("Hello World!");
io.print ("hello world!");
io.write "if Age = <18 no driving"
print "age = 13"
if "a<0" then A = 0 end```