Lua5.1 and 5.2 Impact of sequence definition changes on # table values

Source: Internet
Author: User

Introduction

Environment Lua 5.2

A = {}
For I = 1, 2 do a [I] = I * 3 end


A [4] = 11;
Print (A [# A])


--- Print 11


-----------------------------------


A = {}
For I = 1, 3 do a [I] = I * 3 end


A [5] = 11;
Print (A [# A])

---- Print 9

---------------------------------

# The first value of A is 4. The second value is 3.

Based on the knowledge point I have seen in the book, # The first value of A is 2, and the second value is 3.

Why?

At the beginning, I skipped the source code and did not have a proper keyword search. I just went to the Big Data Group and asked.

After a while, the great god gave me an explanation.

Big Data:

5.1 and 5.2 have changes to sequence definitions. 5.1
2.5.5-the length Operator

The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte ).

The length of a table t is defined to be any integer index n such that t [N] is not nil and T [n + 1] is nil; moreover, if t [1] is nil, N can be zero. for a regular array, with non-Nil values from 1 to a given n, its length is exactly that N, the index of its last value. if the array has "holes" (that is, nil values between other non-Nil values), then # t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array ).


5.2
3.4.6-the length Operator

The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte ).

A program can modify the behavior of the length operator for any value but strings through the _ Len metamethod (see § 2. 4 ).

Unless a _ Len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1 .. n} For Some integer n. in that case, n is its length. note that a table like

{10, 20, nil, 40}
Is not a sequence, because it has the key 4 but does not have the key 3. (So, there is no N such that the set {1 .. n} is equal to the set of positive numeric keys of that table .) note, however, that non-numeric keys do not interfere with whether a table is a sequence.


From a Language Perspective, Lua 5.1 defines # constraints on the length of the array. Lua 5.2 is not strictly defined. It is not determined if nil exists.


That is to say, Lua 5.2 relaxed the language definition and agreed to be more flexible.
Me:
Very advanced.


-- [[Climax part, direct to G point] Big Bang:
Nothing advanced, Lua 5.2 said. If you have nil, don't count on # Table




Dun realized ...... LEVEL + 1

Lua5.1 and 5.2 Impact of sequence definition changes on # table values

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.