Programming in Lua Translation --Types and Values

xiaoxiao2021-03-06  40

2.Types and Valueslua is a dynamic type language. Do not type in variables. 8 basic types in Lua: NIL, Boolean, Number, String, UserData, Function, Thread, and Table. Print (Type ("Hello World")) -> String print (Type (10.4 * 3)) -> Number Print) -> Function Print (Type (Type)) -> Function Print (TRUE) -> Boolean Print (NIL) -> NIL Print (Type (Type (x))) -> String variable does not have a predefined type, each variable may contain any type of value. Print (Type )) -> NIL (`a 'is not initialized) a = 10 print (Type (a)) -> Number a =" a string !! "Print (Type (a)) -> string a = printt - YES, this is Valid! A (Type (a)) -> Function Pay attention to the last two lines above, we can use the function as used as other values. In general, the same variable represents different types of values, It is best not to use, special circumstances, such as NIL.2.1 NIL: Lua special type, giving global variable negative NIL can delete the variable. 2. 2 Booleans: Two values ​​False and True. But pay attention to Lua All values ​​can be used as a condition. In addition to false and nil in the condition of the control structure, the other values ​​are true. So LUA believes that 0 and empty strings are true. 2.3 NumBers: Represents the real number, there is no integer in Lua. Generally, there is a mistake of the CPU operator points. The number is slower than the integer. The truth is not the case, using the fact that the number does not have any error (unless the number is more than 100,000,000,000). Lua's NumBers can handle any long integers. Don't worry. You can also Compose When translation Lua, use long or single-precision characterization instead of NumBers. Digital constants: 4 0.4 4.57E-3 0.3E12 5E 202.4 Lua is 8-bit bytes, so the string can contain any numeric characters, including Embedded 0.

This means that you can store any 2 credit data in a string. Lua strings are not modified, you can create a new variable to store the string you want, as follows: a = "one string" B = String.gsub (a, "one", "another") - Change String Parts Print (a) -> One string print (b) -> ANOTHER STRINGSTRING, LUA automatically distributes memory allocation and Release, a string can only contain one letter or a book, Lua can efficiently process the long string, 1M String is very common in Lua. You can use single quotes or double quotes to represent string a = "a Line "B = 'Another line" For the unified style, it is best to use one unless two quotes are nested. For the case where quotation marks in the string can also be used to represent the escape sequence in Lua. Southern :> Print ("One line / nnext line / n /" in quotes / ", 'in quotes') One line next line" in quotes ", 'in quotes'> Print ('a backslash inside quotes: /' / // ') a backslash inside quotes:' / '> Print ("A Simpler Way:' // ') A Simpler Way:' / 'can also use / DDD in a string (DDD is three decimal numbers ) Means letters. "AlO / N123 /" and '/ 10/04923 "is the same. You can also use [...]] to represent strings. This form of string can contain multiple lines, Can nest, do not explain the escape sequence, such as If the first character is that the community will be automatically ignored. This form of string is used to include a code is very convenient. Page = [ an HTML Page </ Title> </ Head> <body> <a href="http://www.lua.org"> Lua </a> [[a Text Between Double Brackets] </ body> </ html>]] oo.write (PAGE) Running, Lua will automatically perform type conversion between String and NumBers. When a string uses an arithmetic operator, String will be converted into a number. Print ("10" 1) -> 11 Print "10 1") -> 10 1 Print ("- 5.3e - 10" * "2") -></p> <p>-1.06e-09 Print ("Hello" 1) - Error (Cannot "Hello") reverse, when Lua expects a string, the number will be converted to string. Print (10 .. 20 ) -> 1020 .. In Lua is a string connection, when written behind a number .. Must add space to prevent being interpreted. Although strings and numbers can be automatically converted, both are different, like 10 == "10" This is always wrong. If you need to explicitly convert the string into a number, you can use the function tonumber (). If String is not the correct number of the function returns NIL. Line = io.read () - - Read a line n = tonumber (line) - try to communication it to a number if n == nil dam == nil dam == NIL THEN ERROR (line .. "is not a valid number") Else Print (n * 2) end, can be called TOSTRING () converts the number into a string, which has been valid: Print (TOSTRING (10) == "10") -> True Print (10 .. "==" 10 ") -> True2. 5 TableSlua's tables implements the associated array, and the associated array index can not only retrieve data by other values, but also through other types of values. LUA can be subscripted as a TABLES index. Tables is the main The only data structure, we can achieve traditional arrays, symbolic tables, collection, recording (PASCAL), queues, and other data structures. Lua packages are also described by Tables, IO.Read means calling IO The read function in the package means that using a string read as a key to access the IO table. Lua is not a variable is not a value but an object. You can use Tables as an automatically assigned object, only need to manipulate The reference (pointer) of the table can be. LUA does not require a declaration table, you can create a table using the simplest {} expression statement. A = {} - Create a Table and store ITS Reference In `a 'k =" X "a [k ] = 10 - new entry, with key = "x" and value = 10 a [20] = "great" - new entry, with key = 20 and value = "great" print (a ["x"]) -> 10 k = 20 Print (a [k]) -> "great" a ["x"] = a ["x"] 1 - Increme Entry "x" print (a ["x"] ) -> 11 is anonymous, meaning that the table and the variables of the table do not have an inevitable relationship. A = {} a ["x"] = 10 b = a - `b 'refers to the Same Table AS `a 'print (b [" x "]) -> 10 b [" x "] = 20 Print (a [" x "]) -></p> <p>20 a = nil - Now Only `b 'Still Refers to the Table B = NIL - Now The There Are No References Left to the Table When the table is no longer referenced, this table will be deleted, and the memory can be reused. The table can store the value using different index types. The index size increases with the number of elements in the table, adds a = {} - EMPTY TABLE - CREATE 1000 New Entries for i = 1, 1000 do a [i] = i * 2 End Print (a [9]) -> 18 a ["x"] = 10 Print (a ["x"]) -> 10 print (a ["y"]) -> nil last One line, the domain corresponding to the table is not initialized so, like the NIL, and the global variable, Lua's global variable storage is stored using a table. You can use domain name as an index next to the table, Lua also supports A.NAME instead A ["name"], so we can rewrite the above example with a clearer way: AX = 10 - Same as a ["x"] = 10 Print (AX) - Same As Print (a ["X "]) PRINT (AY) - Same as print (a [" Y "]) two ways can be mixed, for LUA, the same is the same, but the single style is more easily understood for the reader. Common Error: Confusion AX and A [X]; the first representation A ["X"], that is, the access domain is a table of the character string "X", the second represents the use of variable X as the index subscript access table element A = {} x = "y" a [x] = 10 - PUT 10 in Field "Y" Print (a [x]) -> 10 - value of field "y" Print (AX) - > NIL - VALUE OF FIELD "X" (Undefined) Print (AY) -> 10 - Value of Field "Y" As long as the integer is used The bid can represent the traditional array, do not need to specify an array size: - Read 10 Lines storing theim in a Table a = {} for i = 1, 10 do a [i] = = rread () End When traversing an array element, the first element that does not initialize is returned to NIL, which can be used as a boundary flag that is subscribed as an array. You can print out the previous example by printing: - Print The Lines for I, LINE In iPAirs (a) Do Print (LINE) End You can use any value as a table's subscript, you can start as an array subscript, but in LUA is generally started instead of 0 (C language) .lua standard The library is also designed. A little needed attention, otherwise you may introduce a lot of difficult BUGs in your code. Because we can use the value of any type as an index, pay attention: Number 0 and String "0" is Different, the same strings " 1", "01", and "1" are also different. I = 10; j = "10"; k = " 10" a =</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-69188.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="69188" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.082</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'YCSAwT_2BZFTT6iql8Nhku9AuVIc4MBbSoqneDi4Sj12FM2TbdJxs3Y6G1uWApWj4HqmHKdNR8a9SuxA6JRCSWWQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>