← Back to Lua Course | Chapter 4: Tables | Lesson 3 of 7

Dictionaries और Keys

string keys या किसी और key के नीचे values store करके एक table को dictionary की तरह इस्तेमाल करें।

In this page:

  1. Dictionaries और Keys

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

lua
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. t.key लिखना जब key एक variable है जो नाम hold करता है
  2. एक missing key के लिए error की उम्मीद करना
  3. यह सोचना कि t["1"] और t[1] same key हैं
चैप्टर सारांश
  • t.k, t["k"] के बराबर है
  • Brackets कोई भी key expression accept करते हैं
  • nil assign करना एक key remove करता है
  • "1" और 1 अलग keys हैं

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.