parseSimpleArray: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
m (Some wiki formatting)
 
(66 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


|arma3dev|= Game name
|game1= arma3
|version1= 1.68


|1.67|= Game version
|gr1= Strings


____________________________________________________________________________________________
|gr2= Arrays


| Converts given, formatted as simple array, [[String]] into a valid [[Array]]. Simple array is array consisting of [[Number]]s, [[String]]s, [[Boolean]]s and [[Array]]s of all of the above. For example: <tt>[1,"2",true,[4,"five",false]]</tt>. The string representation of this array compatible with [[parseSimpleArray]] will be <tt>"[1,""2"",true,[4,""five"",false]]"</tt> accordingly.
|descr= Converts given, formatted as simple array, [[String]] into a valid [[Array]]. Simple array is array consisting of [[Number]]s, [[String]]s, [[Boolean]]s and [[Array]]s of all of the above.
<br><br>
This command is almost '''4&times; faster''' than similar uncached [[call]] [[compile]] method. And because [[call]] [[compile]] is not required, it is also '''more secure''' and primarily intended for use with [[callExtension]] to parse the [[String]] output into [[Array]].
This command is built for speed and security and because of this has several limitations. There is limited error reporting about format errors as the command expects well formatted array as argument. To be able to see exact format expected, you can just copy output result of [[str]] command applied to an array for reference:  <tt>[[copyToClipboard]] [[str]] [[str]] [1,"2",true,[4,"five",false]]</tt> (notice the use of double [[str]] with [[copyToClipboard]]).
<br><br>
In any case here are the explicit rules:
* no spaces are permitted between array elements: <tt>[1,2,3]</tt> - correct, <tt>[1, 2, 3]</tt> - incorrect!
* only double quotes " supported for [[String]]s: <tt>["hello"]</tt> - correct, <tt>['hello']</tt> - incorrect!
* use " to escape ", for example <tt>["hello"]</tt> converted to string should look like this: <tt>"[""hello""]"</tt>
* no spaces should exist before or after array brackets: <tt>"[1,2,3]"</tt> - correct, <tt>"[1,2,3] "</tt> - incorrect!
To simplify, your constructed array format should be the same as Arma array converted to string with [[str]]:
<code>_arr1 <nowiki>=</nowiki> [[getUnitLoadout]] [[player]];
_arr2 <nowiki>=</nowiki> [[parseSimpleArray]] [[str]] _arr1;
[[hint]] [[str]] (_arr1 [[isEqualTo]] _arr2); //true
</code>
Because of this strictness the command is on average 3x faster than similar [[call]] [[compile]] string array method. And because [[call]] [[compile]] is not required, it is also more secure and primarily intended for use with [[callExtension]] to parse the [[String]] output into [[Array]]. |= Description
____________________________________________________________________________________________


| '''parseSimpleArray''' stringArray|= Syntax
{{Feature|informative|
Since {{arma3}} v1.96 the command tolerates extra spaces and supports single quotes. The only recognised keywords (case '''in'''sensitive) are:
{{Columns|2|
* {{hl|true}} - translates into [[true]]
* {{hl|false}} - translates into [[false]]
* {{hl|nil}} - translates into [[nil]]
* {{hl|null}} - translates into [[nil]]
* {{hl|&lt;null&gt;}} - translates into [[nil]] (''not'' [[objNull]])
* {{hl|any}} - translates into [[nil]]
}}
}}


|p1= stringArray: [[String]] - string formatted as simple array |= Parameter 1
|s1= [[parseSimpleArray]] stringArray


| [[Array]] - valid array|= Return value
|p1= stringArray: [[String]] - string formatted as simple array
____________________________________________________________________________________________
 
|x1= <code></code> |= Example 1
|x2= <code></code> |= Example 2
|x3= <code></code>|=
____________________________________________________________________________________________


| [[set]], [[resize]], [[pushBack]], [[pushBackUnique]], [[select]], [[apply]], [[reverse]], [[count]], [[find]], [[in]], [[forEach]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[arrayIntersect]], [[BIS_fnc_arrayPushStack]], [[BIS_fnc_arrayPush]] |= See also
|r1= [[Array]] - valid array


}}
|x1= <sqf>private _arr = parseSimpleArray "[1,2,3]";</sqf>


<h3 style="display:none">Notes</h3>
|x2= <sqf>
<dl class="command_description">
private _array1 = [1, "2", true, [4, "five", false]];
</dl>
private _array2 = parseSimpleArray "[1,""2"",true,[4,""five"",false]]";
_array1 isEqualTo _array2; // true
</sqf>


<h3 style="display:none">Bottom Section</h3>
|x3= <sqf>
_bool = true;
_num = 123.45;
_str = "ok";
_arr = [1, false, "cool"];
_res = parseSimpleArray format ["[%1,%2,%3,%4]", _bool, _num, str _str, str _arr];
// note how _bool and _num do not need str, however if used anyway, the result in this case would be identical
hint str _res;  // returns [true,123.45,"ok",[1,false,"cool"]]
</sqf>


 
|seealso= [[set]] [[resize]] [[pushBack]] [[pushBackUnique]] [[select]] [[apply]] [[reverse]] [[count]] [[find]] [[in]] [[forEach]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[arrayIntersect]] [[callExtension]]
[[Category:Arma_3:_New_Scripting_Commands_List|{{uc:{{PAGENAME}}}}]]
}}
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
 
<!-- CONTINUE Notes -->
 
<!-- DISCONTINUE Notes -->

Latest revision as of 14:22, 29 March 2024

Hover & click on the images for description

Description

Description:
Converts given, formatted as simple array, String into a valid Array. Simple array is array consisting of Numbers, Strings, Booleans and Arrays of all of the above. This command is almost 4× faster than similar uncached call compile method. And because call compile is not required, it is also more secure and primarily intended for use with callExtension to parse the String output into Array.
Since Arma 3 v1.96 the command tolerates extra spaces and supports single quotes. The only recognised keywords (case insensitive) are:
  • true - translates into true
  • false - translates into false
  • nil - translates into nil
  • null - translates into nil
  • <null> - translates into nil (not objNull)
  • any - translates into nil
Groups:
StringsArrays

Syntax

Syntax:
parseSimpleArray stringArray
Parameters:
stringArray: String - string formatted as simple array
Return Value:
Array - valid array

Examples

Example 1:
private _arr = parseSimpleArray "[1,2,3]";
Example 2:
private _array1 = [1, "2", true, [4, "five", false]]; private _array2 = parseSimpleArray "[1,""2"",true,[4,""five"",false]]"; _array1 isEqualTo _array2; // true
Example 3:
_bool = true; _num = 123.45; _str = "ok"; _arr = [1, false, "cool"]; _res = parseSimpleArray format ["[%1,%2,%3,%4]", _bool, _num, str _str, str _arr]; // note how _bool and _num do not need str, however if used anyway, the result in this case would be identical hint str _res; // returns [true,123.45,"ok",[1,false,"cool"]]

Additional Information

See also:
set resize pushBack pushBackUnique select apply reverse count find in forEach deleteAt deleteRange append sort arrayIntersect callExtension

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