Dictionaries और Keys
string keys या किसी और key के नीचे values store करके एक table को dictionary की तरह इस्तेमाल करें।
In this page:
Dictionaries और Keys
String keys के साथ, t.name और t["name"] एक ही चीज़ हैं। Bracket form तब ज़रूरी है जब key एक valid identifier न हो या एक variable से आती हो। एक field को nil set करना इसे delete कर देता है। एक missing key पढ़ना कभी error raise नहीं करता और बस nil देता है।
Note:
variables में held keys के लिए bracket form इस्तेमाल करें, जैसे t[key]।
उदाहरण: Dictionaries and keys
local person = {name = "Ann", age = 30}
person["favorite color"] = "green"
local key = "age"
print(person.name, person[key], person["favorite color"])
person.age = nil
print(person.age)
local t = {}
t[1] = "number"
t["1"] = "string"
print(t[1], t["1"])
-- Output:
-- Ann 30 green
-- nil
-- number string
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- t.key लिखना जब key एक variable है जो नाम hold करता है
- एक missing key के लिए error की उम्मीद करना
- यह सोचना कि t["1"] और t[1] same key हैं
चैप्टर सारांश
- t.k, t["k"] के बराबर है
- Brackets कोई भी key expression accept करते हैं
- nil assign करना एक key remove करता है
- "1" और 1 अलग keys हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: