引子
環境 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
---------------------------------
#a ?第一個的值是4 ? 第二個的值是3 ? ?
依據我之前從書中看到的知識點來看的話 ??#a ?第一個的值是2 ? 第二個的值是3 ? ?才對啊 ?
為啥呢 ?
剛開始入門,源碼部分就跳過吧,又沒有合適的關鍵詞搜索,僅僅能到大嬸群里問了
一會兒 ? 大神來了 ? 給了個牛逼的解釋?
大嬸:
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?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;?
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,
A?program?can?modify?the?behavior?of?the?length?operator?for?any?value?but?strings?through?the?__len?metamethod?
Unless?a?__len?metamethod?is?given,?the?length?of?a?table?t?is?only?defined?if?the?table?is?a?sequence,?that?is,
?????{10,?20,?nil,?40}
is?not?a?sequence,?because?it?has?the?key?4?but?does?not?have?the?key?3.
從語言角度講,?lua?5.1?定義了?#?對數組取長度的約束.?而?lua?5.2?不嚴格定義了,僅僅說假設有?nil?就不確定
也就是說?lua?5.2?放寬了語言定義,同意實現更靈活
?
非常高深啊???我得慢慢啃 ?
--[[高潮部分,直達G點]]
沒什么高深的,?lua?5.2?說,你有?nil?就別指望?#table 對
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
