LUA applies--tables app to find out if it's a reserved word

Source: Internet
Author: User

The table function in Lua is really powerful. Because table is the only data structure in LUA. Today a bit dizzy, less say two more copies of code it.


Suppose you want to list all the identifiers that appear in a source code, in a way, you need to filter out the reserved words of those languages themselves . Some C programmers like to use a string array to represent, all the reserved words in the array, for each identifier into the array to see if it is a reserved word, and sometimes in order to improve query efficiency, the use of binary search or hash algorithm for the array storage .
LUA indicates that this collection has a simple and efficient way to store all the elements in the collection as subscripts in a table , without having to look up the table, just to test to see if the corresponding subscript element value for the given element is nil. Like what:

Reserved ={[" while"] =true, ["End"] =true, ["function"] =true, ["Local"] =true,} forWinchAllwords () Do    ifRESERVED[W] Then    --' W ' is a reserved word...

You can also use helper functions to construct the collection more clearly:

functionSet (list)LocalSet ={} for_, Linch Ipairs(list) DoSET[L] =true End    returnSetEndreserved= set{" while","End","function","Local", }

Well, this way, it's a lot of places.

LUA applies--tables app to find out if it's a reserved word

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: 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.