set: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (command group)
m (Some wiki formatting)
 
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


| ofpr
|game1= ofp
|version1= 1.75


|1.75
|game2= ofpe
|version2= 1.00
 
|game3= arma1
|version3= 1.00
 
|game4= arma2
|version4= 1.00
 
|game5= arma2oa
|version5= 1.50
 
|game6= tkoh
|version6= 1.00
 
|game7= arma3
|version7= 0.50


|gr1= Arrays
|gr1= Arrays
Line 9: Line 26:
|gr2= HashMap
|gr2= HashMap


| Changes the element at the given (zero-based) index of the [[Array|array]].<br>
|descr= Changes the element at the given zero-based index of the [[Array|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 [[set#Examples| Example 4]].
 
{{Feature|informative|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 {{Link|#Example 4}}). }}


| array '''set''' [index, value]
|s1= array [[set]] [index, value]


|p1= array: [[Array]]
|p1= array: [[Array]]
|p2= [index, value]: [[Array]]
|p3= index: [[Number]]
|p4= value: [[Anything]]


| [[Nothing]]
|p2= index: [[Number]] - 0-based array index. {{GVI|arma3|2.12|size= 0.75}} negative index can be used to select from the end of the array, i.e. -1 means last array element.
 
|p3= value: [[Anything]]


| s2= hashMap '''set''' [key, value]
|r1= [[Nothing]]


|p21= hashMap : [[HashMap]] |PARAMETER21=
|s2= hashMap [[set]] [key, value, insertOnly]
|p22= key: [[HashMapKey]] |PARAMETER23=
 
|p23= value: [[Anything]] |PARAMETER24=
|s2since= arma3 2.02
 
|p21= hashMap: [[HashMap]]
 
|p22= key: [[HashMapKey]]
 
|p23= value: [[Anything]]
 
|p24= insertOnly: [[Boolean]] - (Optional, default [[false]]) if set to [[true]], set the value only if the key does not exist already
|p24since= arma3 2.08


|r2= [[Boolean]] - [[false]] if the key is new to the hashmap, [[true]] if a value got overwritten
|r2= [[Boolean]] - [[false]] if the key is new to the hashmap, [[true]] if a value got overwritten
 
|x1= <code>_arrayOne [[set]] [0, "Hello"];</code>


|x2= Append "Bye" as last element to <tt>_arrayTwo</tt>:<br>
|x1= <sqf>_arrayOne set [0, "Hello"];</sqf>
<code>_arrayTwo [[set]] [<nowiki/>[[count]] _arrayTwo, "Bye"];</code>


|x3= Replace the last element of <tt>_arrayThree</tt> with 23:<br>
|x2= Append "Bye" as last element to {{hl|_arrayTwo}}:
<code>_arrayThree [[set]] [<nowiki/>([[count]] _arrayThree) - 1, 23];</code>
<sqf>_arrayTwo set [count _arrayTwo, "Bye"];</sqf>


|x4 = Using [[set]] with an index that is out of bounds:<br>
|x3= Replace the last element of {{hl|_arrayThree}} with 23:
<code>_array = ["A"];
<sqf>_arrayThree set [(count _arrayThree) - 1, 23];</sqf>
_array [[set]] [2, "C"]; {{cc|_array is now ["A", [[nil]], "C"]}}
_array [[set]] [1, "B"]; {{cc|_array is now ["A", "B", "C"]}}
</code>


| [[Array]], [[plus a]], [[a plus b]], [[a - b]], [[resize]], [[reverse]], [[select]], [[in]], [[find]], [[findIf]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[pushBackUnique]], [[apply]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]]
|x4= Using [[set]] with an index that is out of bounds:
<sqf>
private _array = ["A"];
_array set [2, "C"]; // _array is now ["A", nil, "C"]
_array set [1, "B"]; // _array is now ["A", "B", "C"]
</sqf>
 
|x5= <sqf>
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
</sqf>
 
|seealso= [[resize]] [[reverse]] [[select]] [[deleteAt]] [[deleteRange]]  
}}
}}
[[Category:Scripting Commands|SET]]
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|SET]]
{{GameCategory|arma1|Scripting Commands}}
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}

Latest revision as of 20:21, 29 April 2023

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 - 0-based array index. Arma 3 logo black.png2.12 negative index can be used to select from the end of the array, i.e. -1 means last array element.
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