set: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\|x([0-9])= *<code>([^<]*)<\/code>" to "|x$1= <sqf>$2</sqf>")
m (Text replacement - "<code>([^<]*)<nowiki\/?>([^<]*)<\/code>" to "<code>$1$2</code>")
Line 56: Line 56:


|x2= Append "Bye" as last element to {{hl|_arrayTwo}}:
|x2= Append "Bye" as last element to {{hl|_arrayTwo}}:
<code>_arrayTwo set [<nowiki/>count _arrayTwo, "Bye"];</code>
<code>_arrayTwo set [count _arrayTwo, "Bye"];</code>


|x3= Replace the last element of {{hl|_arrayThree}} with 23:
|x3= Replace the last element of {{hl|_arrayThree}} with 23:
<code>_arrayThree set [<nowiki/>(count _arrayThree) - 1, 23];</code>
<code>_arrayThree set [(count _arrayThree) - 1, 23];</code>


|x4= Using [[set]] with an index that is out of bounds:
|x4= Using [[set]] with an index that is out of bounds:

Revision as of 10:28, 13 May 2022

Hover & click on the images for description

Description

Description:
Changes the element at the given zero-based index of the array. If the index is out of bounds, the array will resize to incorporate the index as its last value, padding with nil as necessary (see Example 4).
Groups:
ArraysHashMap

Syntax

Syntax:
array set [index, value]
Parameters:
array: Array
index: Number
value: Anything
Return Value:
Nothing

Alternative Syntax

Syntax:
hashMap set [key, value, insertOnly]
Parameters:
hashMap: HashMap
key: HashMapKey
value: Anything
since Arma 3 logo black.png2.08
insertOnly: Boolean - (Optional, default false) if set to true, set the value only if the key does not exist already
Return Value:
Boolean - False if the key is new to the hashmap, true if a value got overwritten

Examples

Example 1:
_arrayOne set [0, "Hello"];
Example 2:
Append "Bye" as last element to _arrayTwo: _arrayTwo set [count _arrayTwo, "Bye"];
Example 3:
Replace the last element of _arrayThree with 23: _arrayThree set [(count _arrayThree) - 1, 23];
Example 4:
Using set with an index that is out of bounds: private _array = ["A"]; _array set [2, "C"]; // _array is now ["A", nil, "C"] _array set [1, "B"]; // _array is now ["A", "B", "C"]
Example 5:
private _myHashMap = createHashMap; _myHashMap set ["key", "value1", true]; // "key" value is set to "value1" _myHashMap set ["key", "value2", true]; // "key" value is still "value1" as "key" already exists in the hashmap

Additional Information

See also:
resize reverse select deleteAt deleteRange

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note