Usage of tables in LUA scripts

Source: Internet
Author: User
Tags lua
--[[LUA table processing]]--alice = {} alice["name"] = "Liuhui" alice["phone"] = "15157121639" alice["address"] = "Hangzhou Jianggan" Alic e["City" = "Hangzhou" Print (alice["name"]) print (alice["City") print (alice.city)--[[with {} processing assignment after each statement,]]--m Ytab = {["name"] = "Liuhui", ["xingbie"] = "male", ["Age"] =24,} mytab1 = {name = "Xiaohui", email = "liuhui331234958@12 6.com ",} print (Mytab.name) print (mytab1.email)--[[use the table as an array]]--tab1 = {" abc "," Def "," Mok "," Liuhui ",} tab2 = {[1] = "abc", [2] = "Def", [3] = "Mok",}--[[Lua array is calculated from 1]]----[[Print (tab1[1])]]--print ("TAB1 print:") len = #tab1 for I=1, Len do print (tab1[i]) i = i + 1 end print ("TaB2 printing:") len = #tab2 for I=1, Len do print (tab2[i]) i = i + 1 End tab1[5] = "hehe" Print (Tab1[4]) print (tab1[5])--[[more standard array add element procedure Table.insert If no second argument is added to end]]--table.in SERT (TAB1, 6, "Nihao") print (tab1[6])--[[remove element Table.remove from the array, similar to Table.insert]]--print ("delete tab1[1]") print ("before deleting Length is: ". #tab1: " Content is ") foR I=1, #tab1 do print (Tab1[i]) end Table.remove (TAB1, 1) print ("After delete length:" ...) #tab1)--[[The second element in the original array is shown here, the default is to move forward one]]--for I=1, #tab1 do print (Tab1[i]) end Table.remove (tab1)--[[Delete without the delete label defaults to The last element deletes]]--print ("Deleted length:" ...).  #tab1) for I=1, #tab1 the Do print (Tab1[i]) end--[[array to sort]]--array = {"Liuhui", "haha", "abc", "Zero",} table.sort ( Array) for I=1, #array do print (Array[i]) end--[[array Add function name]]--function prtmsg (str) Local msg = str return msg E nd function Prttype (str) Local ret = "" if (type (str) = = "string") then ret = "string" Return ret ElseIf (type (str) = = "Double") then ret = "Double" return ret else return ret end end util = {} Util.func = prtmsg Util.type = prttype m str = "Hello" print ("message type:"). Util.type (MSTR)) print ("Message info:"). Util.func ("Hello"))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.