You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Changes
* Removes reccomendation of `pairs()` and replaces it with generic
iteration
* Renames the deepCopy function to deepClone
* Makes the deepClone function use table.clone
* Fix deepFreeze erroring if a value is already frozen
* Explains dot indexing for dictionaries `.key`
* Removes warning for using ipairs to iterate over a dictionary, as
ipairs is unnessary to use unless wanting to stop at the first nil in a
array with holes (a behavior that is very rarely desired)
The last one is a bit iffy so feel free to reverse it. Also unsure if
the wording for `Write to dictionaries` is the best, but I think the
added warning clears it up.
## Checks
By submitting your pull request for review, you agree to the following:
- [x] This contribution was created in whole or in part by me, and I
have the right to submit it under the terms of this repository's open
source licenses.
- [x] I understand and agree that this contribution and a record of it
are public, maintained indefinitely, and may be redistributed under the
terms of this repository's open source licenses.
- [x] To the best of my knowledge, all proposed changes are accurate.
---------
Copy file name to clipboardExpand all lines: content/en-us/luau/tables.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -149,7 +149,7 @@ local testDictionary = {
149
149
150
150
### Read from dictionaries
151
151
152
-
To read from a dictionary, add a pair of brackets after its reference and specify the key name. Directly reference a string key using quotes (`["key"]`) or use a variable value (`[key]`).
152
+
To read from a dictionary, add a pair of brackets after its reference and specify the key name. Directly reference a string key using either (`["key"]`) or (`.key`), or instead use a variable value (`[key]`).
153
153
154
154
```lua
155
155
localpart=Instance.new("Part")
@@ -160,13 +160,15 @@ local testDictionary = {
160
160
}
161
161
-- Include quotes for string keys
162
162
print(testDictionary["partType"]) -- Block
163
+
-- Or use . to index string keys without spaces
164
+
print(testDictionary.partType) -- Block
163
165
-- Omit quotes for non-string keys
164
166
print(testDictionary[part]) -- true
165
167
```
166
168
167
169
### Write to dictionaries
168
170
169
-
To define or rewrite the value of a new or existing dictionary key, declare the key name in brackets (`[key]`) followed by `=` and the value:
171
+
To define or rewrite the value of a new or existing dictionary key, declare the key name in brackets (`[key]`) or, if the key is a string, use (`.key`) followed by `=` and the value:
170
172
171
173
```lua
172
174
localtestDictionary= {
@@ -176,19 +178,19 @@ local testDictionary = {
176
178
177
179
-- Change value of existing keys
178
180
testDictionary["fruitName"] ="Cherry"
179
-
testDictionary["sour"]=false
181
+
testDictionary.sour=false
180
182
181
183
-- Insert new key-value pair
182
-
testDictionary["fruitCount"]=10
184
+
testDictionary.fruitCount=10
183
185
184
-
print(testDictionary["fruitName"]) -- Cherry
185
-
print(testDictionary["sour"]) -- false
186
-
print(testDictionary["fruitCount"]) -- 10
186
+
print(testDictionary.fruitName) -- Cherry
187
+
print(testDictionary.sour) -- false
188
+
print(testDictionary.fruitCount) -- 10
187
189
```
188
190
189
191
### Iterate over dictionaries
190
192
191
-
To iterate over a dictionary, use the global `pairs()` function in a `for` loop:
193
+
To iterate over a dictionary, use a `for` loop:
192
194
193
195
```lua
194
196
localtestDictionary= {
@@ -197,7 +199,7 @@ local testDictionary = {
197
199
sour=true
198
200
}
199
201
200
-
forkey, valueinpairs(testDictionary)do
202
+
forkey, valueintestDictionarydo
201
203
print(key, value)
202
204
end
203
205
@@ -208,10 +210,6 @@ fruitColor Yellow
208
210
]]
209
211
```
210
212
211
-
<Alertseverity="warning">
212
-
Unlike using `ipairs()` on an array, using `pairs()` on a dictionary doesn't necessarily return items in the same order that they're in the dictionary.
213
-
</Alert>
214
-
215
213
### Remove key-value pairs
216
214
217
215
To remove or erase a key-value pair from a dictionary, set its value for a key to `nil`.
@@ -223,9 +221,9 @@ local testDictionary = {
223
221
sour=true
224
222
}
225
223
226
-
testDictionary["sour"]=nil
224
+
testDictionary.sour=nil
227
225
228
-
forkey, valueinpairs(testDictionary)do
226
+
forkey, valueintestDictionarydo
229
227
print(key, value)
230
228
end
231
229
--[[ Resulting output:
@@ -280,20 +278,21 @@ local clone = table.clone(original)
280
278
To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following:
281
279
282
280
```lua
283
-
-- The function used for deep copying a table
284
-
localfunctiondeepCopy(original)
281
+
-- The function used for deep cloning a table
282
+
localfunctiondeepClone(original)
285
283
-- Define the new table for the copy
286
-
localcopy={}
284
+
localclone=table.clone(original)
287
285
288
-
-- Loop through the original table to clone
286
+
-- Loop through the original table to check for table values
287
+
-- If a table is found as a value, deep clone it to the key (index)
289
288
forkey, valueinoriginaldo
290
-
-- If the type of the value is a table, deep copy it to the key (index)
291
-
-- Else (or) the type isn't a table, assign the default value to the index instead
0 commit comments