+: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
(Fix note and add example)
Line 15: Line 15:
| Add two numbers, concatenate two arrays or two strings, or duplicate an array. For arrays also see [[pushBack]] and [[append]].  
| Add two numbers, concatenate two arrays or two strings, or duplicate an array. For arrays also see [[pushBack]] and [[append]].  


{{Warning | Making copy with <tt>ARRAY + []</tt> and <tt>+ARRAY</tt> are not the same thing. <tt>+ARRAY</tt> clones every element, so if the array is multi-dimensional, the created copy contains no references. <tt>ARRAY + []</tt> creates copy of the 1st dimension, but preserves references in other dimensions. Observe:
{{Warning | [[Array]] copy methods <tt>ARRAY + []</tt> and <tt>+ARRAY</tt> have different behaviours:
<code>arr1 {{=}} [1,2,3]; arr2 {{=}} [arr1,1,2,3]; arr3 {{=}} arr2 + []; arr3 [[select]] 0 [[set]] [0, "oops"]; [[hint]] [[str]] arr1; // ["oops",2,3]</code>
* <tt>ARRAY + []</tt> creates copy of the 1st dimension, but preserves references in other dimensions (shallow copy).
vs.
* <tt>+ARRAY</tt> clones every element, so if the array is multi-dimensional, the created copy contains no references (deep copy).
<code>arr1 {{=}} [1,2,3]; arr2 {{=}} [arr1,1,2,3]; arr3 {{=}} +arr2; arr3 [[select]] 0 [[set]] [0, "oops"]; [[hint]] [[str]] arr1; // [1,2,3]</code>}} |Description=
See [[#Examples|Example 6]].}} |Description=
____________________________________________________________________________________________
____________________________________________________________________________________________


Line 78: Line 78:
_arrayB = +_arrayA;
_arrayB = +_arrayA;
_arrayB [[set]] [0, 99]; {{codecomment|// _arrayA {{=}} [1,2,3], _arrayB {{=}} [99,2,3]}}</code> |Example 5=
_arrayB [[set]] [0, 99]; {{codecomment|// _arrayA {{=}} [1,2,3], _arrayB {{=}} [99,2,3]}}</code> |Example 5=
|x6= Shallow copy with <tt>ARRAY + []</tt>:
<code>[[private]] _subArray {{=}} [1, 2, 3];
[[private]] _array1 {{=}} [_subArray, 1, 2, 3];
[[private]] _array2 {{=}} _array1 + [];
_array2 [[select]] 0 [[set]] [0, "oops"];
[[hint]] [[str]] _subArray; {{cc|["oops", 2, 3]}}</code>
Deep copy with <tt>+ARRAY</tt>:
<code>[[private]] _subArray {{=}} [1, 2, 3];
[[private]] _array1 {{=}} [_subArray, 1, 2, 3];
[[private]] _array2 {{=}} +_array1;
_array2 [[select]] 0 [[set]] [0, "oops"];
[[hint]] [[str]] _subArray; {{cc|[1, 2, 3]}}</code> |Example 6=
____________________________________________________________________________________________
____________________________________________________________________________________________



Revision as of 12:02, 6 August 2020

Hover & click on the images for description

Description

Description:
Add two numbers, concatenate two arrays or two strings, or duplicate an array. For arrays also see pushBack and append.
Array copy methods ARRAY + [] and +ARRAY have different behaviours:
  • ARRAY + [] creates copy of the 1st dimension, but preserves references in other dimensions (shallow copy).
  • +ARRAY clones every element, so if the array is multi-dimensional, the created copy contains no references (deep copy).
See Example 6.
Groups:
Uncategorised

Syntax 1

Syntax:
numberA + numberB
Parameters:
numberA: Number
numberB: Number
Return Value:
Number - the sum of numberA and numberB

Syntax 2

Syntax:
+ numberA
Parameters:
numberA: Number
Return Value:
Number - returns numberA

Syntax 3

Syntax:
arrayA + arrayB
Parameters:
arrayA: Array
arrayB: Array
Return Value:
Array - a new array filled with arrayA and arrayB elements, in that order.

Syntax 4

Syntax:
stringA + stringB
Parameters:
stringA: String
stringB: String
Return Value:
String - a string concatenating stringA and stringB

Syntax 5

Syntax:
+ arrayA
Parameters:
arrayA: Array
Return Value:
Array - a new instance of array filled with same items.

Examples

Example 1:
5 + 3; // returns 8 -5 + -3; // returns -8
Example 2:
+ 2; // returns 2 + -7; // returns -7
Example 3:
_arrayA = [1,2,3]; _arrayB = [3,4,5]; _arrayAB = _arrayA + _arrayB; // _arrayAB = [1,2,3,3,4,5] // _arrayA and _arrayB remain unchanged
Example 4:
_result = "Hello" + " " + "there"; // "Hello there"
Example 5:
_arrayA = [1,2,3]; _arrayB = +_arrayA; _arrayB set [0, 99]; // _arrayA = [1,2,3], _arrayB = [99,2,3]
Example 6:
Shallow copy with ARRAY + []: private _subArray = [1, 2, 3]; private _array1 = [_subArray, 1, 2, 3]; private _array2 = _array1 + []; _array2 select 0 set [0, "oops"]; hint str _subArray; // ["oops", 2, 3] Deep copy with +ARRAY: private _subArray = [1, 2, 3]; private _array1 = [_subArray, 1, 2, 3]; private _array2 = +_array1; _array2 select 0 set [0, "oops"]; hint str _subArray; // [1, 2, 3]

Additional Information

See also:
OperatorspushBackappend

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

Bottom Section