Re: Number 2 String( LUA_NOCVTN2S ) and numbers and concat..., Roberto Ierusalimschy Prev by Date: Re: lua.pc pkg-config file Next by Date: Re: Number 2 String( LUA_NOCVTN2S ) and numbers and concat...
For guys who visit this article by "Lua how to join strings" request thought second link from Google. And who seeks for Array.join(' ') Ruby/JS analog. It is a table.concat(table [, sep [, i [, j]]]) in Lua. – Dmitry Dmitriev Jun 26 at 15:56 lua - table.concat with string keys Tag: lua , lua-table I'm having a problem with lua's table.concat, and suspect it is just my ignorance, but cannot find a verbose answer to why I'm getting this behavior. Lua also converts strings to numbers if the string is such that it can represent a number and it's used in an arithmetic operation. Likewise, Lua converts a number to a string if the number is used in a string operation such as the .. concatenation operator. #!/usr/bin/lua a = 1 -- this is a number b = 10 -- this is a number This is Lua version 5.1 we are using. > = "Pi = ".. math.pi Pi = 3.1415926535898 > = "Pi = ".. 3.1415927 Pi = 3.1415927 As shown above, during coercion, we do not have full control over the formatting of the conversion. To format the number as a string as we would like we can use the string.format() function. e.g., Asimpletable.concattest A simple table.concat test table.concat test. Page views: 24,982 Updated: Last Month Playing
An Introduction to Tables A table is a way to store multiple values in one variable. Essentially, it is a list in which each item has 2 parts: (1) a key (also called an index ), which is the name of the item, a way to tell the table which thing you want to look at; and (2) the value , which is the actual data. The Lua way is to put the strings into a table and then use table.concat. If you are a C programmer, this may seem massively inefficient, but the answer is that Lua strings are not the right data structure for modification. Representing a string as a table of string fragments (sometimes called a 'rope') is much more efficient. 10 Using metatables
Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format. The most concise would be to let Lua do most of the parsing for us. We'll first chop the string into words, then chop each word into characters, convert the characters, concat the characters back into a word with the character delimiter, then concat the words back into a string with the word delimiter: Ususally it will convert characters into their ASCII number. There is a warning in the documentation though that "Numerical codes are not necessarily portable across platforms". But as long as we're talking about the environment of ComputerCraft Lua I don't think this will be a problem at all.