I can set the prompt with _PROMPT = "> ", but can I make the prompt update every time?
I tried this, but it doesn't work:
i = 0
function inc()
i = i + 1
return i
end
_PROMPT = inc
This shows _PROMPT, but nothing related:
for k, v in pairs(_G) do
print(k)
end
The primary prompt is the value of the global variable _PROMPT, if this value is a string; otherwise, the default prompt is used.
https://www.lua.org/manual/5.1/lua.html
You assigned a function to _PROMPT.
I tried
_PROMPT = {no = 0}; setmetatable (_PROMPT, {__tostring = function (self) self.no = self.no + 1; return tostring (self.no) .. ' >' end})
, but no luck, although _PROMPT was incremented every time I typed = _PROMPT.
UPD Yet this can be done! In Lua mailing list I was advised to use
setmetatable(_ENV, {__index = function(t, k) if k == '_PROMPT' then t._N = (t._N or 0) + 1; return t._N .. ' >' end end})
It works. This effectively is a way to override any global in a deeper sense than simply to assign a new value to it, or make a set of global variables effectively infinite.
You can start Lua in interactive mode with a one-liner:
lua -i -e "setmetatable(_ENV, {__index = function(t, k) if k == '_PROMPT' then t._N = (t._N or 0) + 1; return t._N .. ' >' end end})"
I found out a method with debug.sethook().
It sounds a bit strange but it is really simple ;-)
Here we go...
# /usr/bin/lua -i
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> debug.sethook(function(...) _PROMPT=os.date('%H:%M:%S # ') end,'r')
10:49:42 # -- Hiting ENTER some times
10:51:00 #
10:51:01 #
10:51:05 #
( Done with Lua 5.3.5 and tested 5.4 - Should work with 5.1 but not tested )
The 'r' means: Fire on each return
EDIT
Another way directly with _PROMPT and _PROMPT2...
-- Simple method for changing and/or time logging the prompts
_PROMPT=setmetatable({},{__index=table})
_PROMPT2=setmetatable({},{__index=table})
getmetatable(_PROMPT).__tostring=function()
_PROMPT:insert(os.date('%H:%M:%S',os.time())..'> ')
return string.format('%s',_PROMPT:concat(#_PROMPT,#_PROMPT))
end
getmetatable(_PROMPT2).__tostring=function()
_PROMPT2:insert(os.date('%H:%M:%S',os.time())..'>> ')
return string.format('%s',_PROMPT2:concat(#_PROMPT2,#_PROMPT2))
end
...using __tostring and __index has table metamethods.
...much fun and stay healthy.
Related
The following is string of arithmetic operations in lua.
local str ='x+abc*def+y^z+10'
Can this string be splitted so that individual variables or numbers will appear? For example, say string str is splitted into table s. Then the output will be
s[1] = x
s[2] = abc
s[3] = def
s[4] = y
s[5] = z
s[6] = 10
The splitting is to be done with operators +,-,*,\,^,%
Try also this simpler pattern:
local str ='x+(abc*def)+y^z+10'
for w in str:gmatch("%w+") do
print(w)
end
You can use string.gmatch to iterate over your string.
Feel free to add other operators to the pattern.
Refer to https://www.lua.org/manual/5.3/manual.html#6.4.1
local str ='x+abc*def+y^z+10'
local s = {}
for operand in str:gmatch('[^%+%*%^]+') do
table.insert(s, operand)
end
You can use string.gmatch to do what your looking for. you would use the pattern %+%-%*%^/
local str ='x+abc*def+y^z+10'
local s = {}
for value in str:gmatch("[%+%-%*%^/]*(%w*)[%+%-%*%^/]*") do
s[#s + 1] = value
end
print(unpack(s))
Also not that if you need \ as operator as shown in your question it would need to be escaped using an additional \.
Resourse for leaning more about lua patterns: understanding_lua_patterns
I encountered this bug in cjson lua when I was using a script in redis 3.2 to set a particular value in a json object.
Currently, the lua in redis does not differentiate between an empty json array or an empty json object. Which causes serious problems when serialising json objects that have arrays within them.
eval "local json_str = '{\"items\":[],\"properties\":{}}' return cjson.encode(cjson.decode(json_str))" 0
Result:
"{\"items\":{},\"properties\":{}}"
I found this solution https://github.com/mpx/lua-cjson/issues/11 but I wasn't able to implement in a redis script.
This is an unsuccessful attempt :
eval
"function cjson.mark_as_array(t)
local mt = getmetatable(t) or {}
mt.__is_cjson_array = true
return setmetatable(t, mt)
end
function cjson.is_marked_as_array(t)
local mt = getmetatable(t)
return mt and mt.__is_cjson_array end
local json_str = '{\"items\":[],\"properties\":{}}'
return cjson.encode(cjson.decode(json_str))"
0
Any help or pointer appreciated.
There are two plans.
Modify the lua-cjson source code and compile redis, click here for details.
Fix by code:
local now = redis.call("time")
-- local timestamp = tonumber(now[1]) * 1000 + math.floor(now[2]/1000)
math.randomseed(now[2])
local emptyFlag = "empty_" .. now[1] .. "_" .. now[2] .. "_" .. math.random(10000)
local emptyArrays = {}
local function emptyArray()
if cjson.as_array then
-- cjson fixed: https://github.com/xiyuan-fengyu/redis-lua-cjson-empty-table-fix
local arr = {}
setmetatable(arr, cjson.as_array)
return arr
else
-- plan 2
local arr = {}
table.insert(emptyArrays, arr)
return arr
end
end
local function toJsonStr(obj)
if #emptyArrays > 0 then
-- plan 2
for i, item in ipairs(emptyArrays) do
if #item == 0 then
-- empty array, insert a special mark
table.insert(item, 1, emptyFlag)
end
end
local jsonStr = cjson.encode(obj)
-- replace empty array
jsonStr = (string.gsub(jsonStr, '%["' .. emptyFlag .. '"]', "[]"))
for i, item in ipairs(emptyArrays) do
if item[1] == emptyFlag then
table.remove(item, 1)
end
end
return jsonStr
else
return cjson.encode(obj)
end
end
-- example
local arr = emptyArray()
local str = toJsonStr(arr)
print(str) -- "[]"
I'm trying to save some data in to a table. I get the data from a database and it works ok.
My problem is that the data is not saved in the table. It is a lua table like table = {} and NOT a database table.
Maybe it is saved but it looks like the prints are done before the saving even though I call them after. In fact it seems like my network request is done last in my program even though I call it first.
I would real like to know the reason for this. Any ideas?
Here is the code:
---TESTING!
print("Begin teting!")
--hej = require ( "test2" )
local navTable = {
Eng_Spd = 0,
Spd_Set = 0
}
local changeTab = function()
navTable.Eng_Spd = 2
end
printNavTable = function()
print("navTable innehåller: ")
print(navTable.Eng_Spd)
print(navTable.Spd_Set)
end
require "sqlite3"
local myNewData
local json = require ("json")
local decodedData
local SaveData2 = function()
local i = 1
local counter = 1
local index = "livedata"..counter
local navValue = decodedData[index]
print(navValue)
while (navValue ~=nil) do
--tablefill ="INSERT INTO navaltable VALUES (NULL,'" .. navValue[1] .. "','" .. navValue[3] .."','" .. navValue[4] .."','" .. navValue[5] .."','" .. navValue[6] .."');"
--print(tablefill)
--db:exec(tablefill)
if navValue[3] == "Eng Spd" then navTable.Eng_Spd = navValue[4]
elseif navValue[3] == "Spd Set" then navTable.Spd_Set = navValue[4]
else print("blah")
end
print(navTable.Eng_Spd)
print(navTable.Spd_Set)
counter=counter+1
index = "livedata"..counter
navValue = decodedData[index]
end
end
local function networkListener( event )
if (event.isError) then
print("Network error!")
else
myNewData = event.response
print("From server: "..myNewData)
decodedData = (json.decode(myNewData))
SaveData2()
--db:exec("DROP TABLE IN EXISTS navaltable")
end
end
--function uppdateNavalTable()
network.request( "http://127.0.0.1/firstMidle.php", "GET", networkListener )
--end
changeTab()
printNavTable()
--uppdateNavalTable()
printNavTable()
print("Done!")
And here is the output:
Copyright (C) 2009-2012 C o r o n a L a b s I n c .
Version: 2.0.0
Build: 2012.971
Begin teting!
navTable innehåller:
2
0
navTable innehåller:
2
0
Done!
From server: {"livedata1":["1","0","Eng Spd","30","0","2013-03-15 11:35:48"],"li
vedata2":["1","1","Spd Set","13","0","2013-03-15 11:35:37"]}
table: 008B5018
30
0
30
13
And btw, navTable innehåller means navTable contains.
The answer is that networklistener run parallell with the rest of the code.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
LZW Compression In Lua
Here is my code for compressing data in Lua using the LZW compression method. My problem is that the function is returning the character 'T', instead of returning the full compressed string 'TOBEORNOTTOBEORNOT'. Thanks!
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for i = 1, #uncompressed do
local c = string.sub(uncompressed, i, i)
local wc = w .. c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
if w then
dictionary[w] = ""
end
return w
end
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print(compressed)
Just a hint: you return w in the for loop
EDIT some explanation
If you return your result in the loop, then the loop will do only one iteration. At the end of the first iteration, your function will finish. That makes no sense. So your return statement should come after the for loop.
Also, it is suspicious that you declare a variable result = {} and then you never use it.
So I suggest you put your return statement after the loop and you print the value of your variables at the end of in each iteration (you'd put the print statements where you have the return now), so you can see what is really happening.
Here is the Pseudocode for Lempel-Ziv-Welch Compression.
pattern = get input character
while ( not end-of-file ) {
K = get input character
if ( <<pattern, K>> is NOT in
the string table ){
output the code for pattern
add <<pattern, K>> to the string table
pattern = K
}
else { pattern = <<pattern, K>> }
}
output the code for pattern
output EOF_CODE
I am trying to code this in Lua, but it is not really working. Here is the code I modeled after an LZW function in Python, but I am getting an "attempt to call a string value" error on line 8.
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for c in uncompressed do
-- while c is in the function compress
local wc = w + c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
-- Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
-- Output the code for w.
if w then
dictionary[w] = ""
end
end
return dictionary
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print (compressed)
I would really like some help either getting my code to run, or helping me code the LZW compression in Lua. Thank you so much!
Assuming uncompressed is a string, you'll need to use something like this to iterate over it:
for i = 1, #uncompressed do
local c = string.sub(uncompressed, i, i)
-- etc
end
There's another issue on line 10; .. is used for string concatenation in Lua, so this line should be local wc = w .. c.
You may also want to read this with regard to the performance of string concatenation. Long story short, it's often more efficient to keep each element in a table and return it with table.concat().
You should also take a look here to download the source for a high-performance LZW compression algorithm in Lua...