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

Tables बनाना

एक table एक container है जो keys के नीचे values store करता है, और यह एकमात्र data structure है जो Lua के पास है।

In this page:

  1. Tables बनाना

Tables बनाना

Tables curly braces से बनाई जाती हैं और nil को छोड़कर किसी भी value को key की तरह hold कर सकती हैं। same table positional items और named fields mix कर सकता है। Tables objects हैं, इसलिए एक table को दूसरे variable में assign करना इसे copy करने की बजाय share करता है।

Note: क्योंकि tables Lua का एकमात्र structure हैं, arrays, records, sets, और modules सब बस tables हैं।

उदाहरण: Creating tables

lua
local t = {10, 20, name = "box", [3.5] = "float key"}
print(t[1], t[2], t.name, t[3.5])
print(t.missing)
local alias = t
alias.name = "renamed"
print(t.name)
print(type(t), #{})

-- Output:
-- 10	20	box	float key
-- nil
-- renamed
-- table	0
{# 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. यह उम्मीद करना कि assign होने पर एक table variable एक copy hold करेगा
  2. nil को एक key की तरह इस्तेमाल करना
  3. constructor {} को एक block से confuse करना
चैप्टर सारांश
  • Tables {} से बनती हैं
  • nil को छोड़कर कोई भी value एक key हो सकती है
  • एक table arrays और fields mix कर सकता है
  • Assignment reference copy करता है

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.