https://github.com/ahmedabougabal/lua_script_ivr
https://github.com/ahmedabougabal/lua_script_ivr
bash command-line ivr linux lua
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ahmedabougabal/lua_script_ivr
- Owner: ahmedabougabal
- Created: 2024-10-31T17:49:02.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-01T21:15:48.000Z (7 months ago)
- Last Synced: 2025-04-09T21:52:22.374Z (about 2 months ago)
- Topics: bash, command-line, ivr, linux, lua
- Language: Lua
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lua_Script_IVR
# Comprehensive Guide to Lua Programming Concepts
## 1. Understanding nil and Tables
### The True Nature of nil
In Lua, `nil` is not just another value - it represents the absence of a value. This distinction is crucial:```lua
-- This creates a table with explicit holes
local t = {1, nil, 3}
print(#t) -- Output might be 1, not 3!-- This creates a proper sequential table
local t2 = {1, 2, 3}
t2[2] = nil -- Now we've created a hole
print(#t2) -- Output might be 1!
```### Length Operator Behavior
The length operator (`#`) finds the first numeric index `n` where `t[n]` is nil and `t[n+1]` is not nil. This means:```lua
local t = {1, nil, 3}
-- The length might be 1 because:
-- t[1] = 1
-- t[2] = nil (found our nil!)
-- t[3] = 3 (but this doesn't matter for the length calculation)-- Correct way to create a sequence:
local t = {1, 2, 3}
-- Now #t will reliably be 3
```## 2. Print Function Behavior
The `print()` function automatically adds tabs between multiple arguments:
```lua
print("Hello", "World") -- Output: Hello World
print("Hello" .. "World") -- Output: HelloWorld-- When using varargs:
function demo(...)
print("Start", ...) -- Tabs between each value
end
demo("a", "b", "c") -- Output: Start a b c
```## 3. Varargs (...) Behavior
The `...` operator represents a sequence of values, not a single value:
```lua
function example(...)
-- INCORRECT:
local args = ... -- This only gets the first value
-- CORRECT:
local args = {...} -- This creates a table with all values
-- Access individual values:
local first = select(1, ...)
local count = select('#', ...)
end
```## 4. Variable Assignment and Memory Management
### Understanding '_' Convention
The underscore is just a convention for unused variables:```lua
-- This doesn't discard the value:
local _ = expensive_function() -- Value is still computed-- All values are still computed:
local _, value = multiple_returns()
```### Memory Management
Setting variables to nil allows garbage collection:```lua
local large_data = create_large_table()
-- When done with the data:
large_data = nil -- Now eligible for garbage collection-- For tables, you might want to clear references:
function clear_table(t)
for k in pairs(t) do
t[k] = nil
end
end
```## 5. For Loops
### Numeric For Loop
```lua
-- Basic syntax: for var = start, end, step do
for i = 1, 10 do
print(i)
end-- With step value
for i = 10, 1, -2 do -- Counts down by 2
print(i)
end
```### Generic For Loop
```lua
-- pairs() - iterates over all table keys
for k, v in pairs(table) do
-- k is the key (any type)
-- v is the value
end-- ipairs() - iterates over numeric indices only
for i, v in ipairs(table) do
-- i is always a number
-- v is the value
end
```## 6. Table Operations
### Table Creation and Modification
```lua
-- Initial creation
local t = {
name = "John", -- This creates t["name"]
[1] = "first", -- Numeric index
["key"] = "value" -- Explicit string key
}-- Later modification
t.age = 25 -- Same as t["age"] = 25
t["score"] = 100 -- Explicit string key
t[1] = "new" -- Numeric index
```### Ordered Iteration
```lua
-- DON'T rely on pairs() for ordered iteration
table.sort(t)
-- This might work but isn't guaranteed:
for k, v in pairs(t) do end-- DO use numeric for loop for ordered iteration:
for i = 1, #t do
print(t[i])
end
```## 7. Random Number Generation
### Proper Seeding
```lua
-- Basic seeding (NOT recommended for security-critical code)
math.randomseed(os.time())-- Better seeding for security-critical applications
local function better_seed()
local socket = require("socket") -- If available
return math.randomseed(socket.gettime() * 10000)
end
```## 8. Function Definitions
### Function Declaration Syntax
```lua
-- These are equivalent:
function foo(x)
return x * 2
end-- This shows what's actually happening:
foo = function(x)
return x * 2
end
```## 9. Multiline Strings
### Special Behaviors
```lua
-- Escape sequences are not interpreted:
local str1 = [[
This \n will appear literally
]]-- First newline is ignored:
local str2 = [[
This text starts on this line
not the line above
]]-- With equals signs for nesting:
local str3 = [=[
Can contain regular [[brackets]]
]=]
```# 💯❤️ Acknowledgments
> thanks for @biggerdoofus and Trevor Sullivan