background preloader

Lua

Facebook Twitter

Wiki: Table Utils - Nightly. Convert a table to a string Produces a compact, uncluttered representation of a table. Mutual recursion is employed. function table.val_to_str ( v ) if "string" == type( v ) then v = string.gsub( v, "\n", "\\n" ) if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then return "'" .. v .. "'" end return '"' .. string.gsub(v,'"', '\\"' ) .. '"' else return "table" == type( v ) and table.tostring( v ) or tostring( v ) endend function table.key_to_str ( k ) if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then return k else return "[" .. table.val_to_str( k ) ..

"]" endend function table.tostring( tbl ) local result, done = {}, {} for k, v in ipairs( tbl ) do table.insert( result, table.val_to_str( v ) ) done[ k ] = true end for k, v in pairs( tbl ) do if not done[ k ] then table.insert( result, table.key_to_str( k ) .. "=" .. table.val_to_str( v ) ) end end return "{" .. table.concat( result, "," ) .. "}"end Example: This prints {11,22,33,{"a","b"},foo="bar"} . --DavidManura. Language : lua - page 1 - Code Snippets Collection - Nightly. Wiki: Lua Unicode - Nightly. This is an attempt to answer the LuaFaq : Can I use unicode strings? Or Does Lua support unicode? In short, yes and no. Lua is unicode-agnostic and lua-strings are counted, so whenever you can treat unicode strings as simple byte sequences, you are done.

Some of the issues are: Can I store, retrieve and concatenate Unicode strings? Unicode strings and Lua strings A Lua string is an arbitrary counted sequence of bytes (C chars of your compiler, so 8 bit or bigger). For best results, use an encoding with unicode codeunits no bigger than a single byte, which normally restricts you to utf8. Input and output of strings in Lua (using the io library) conforms to C's guarantees.

This may affect your ability to do non-binary file input and output of Unicode. All modern systems only do minimal byte-sequence mapping for line-endings in textmode, unix going so far as not needing even that and so making text and binary mode identical. Unicode Lua programs Comparison and Sorting Pattern Matching See Also. Wiki: Split Join - Nightly. "split" [1] and "join" [2] are two common string operators that are essentially inverse operations of each other. Split separates a string containing a delimiter into the list of substrings between that delimiter. Join combines a list of strings into a new string by inserting a delimiter between each string. There are various ways to design and implement these functions in Lua, as described below.

Joining list of strings With Lua 5.x you can use table.concat[3] for joining: table.concat(tbl, delimiter_str). table.concat({"a", "b", "c"}, ",") Other interfaces are possible, largely dependent on the choice of split interface since join is often intended to be the inverse operation of split. Splitting Strings First of all, although Lua does not have a split function is its standard library, it does have string.gmatch[4], which can be used instead of a split function in many cases. Local example = "an example string"for i in string.gmatch(example, "%S+") do print(i) end split("a,b,c", ",", 2) Fix:

Luai: Lua reference manual helper: all Lua bits - Nightly. Wiki: Sample Code - Nightly.