You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
== array_get ==
[[../objects/ms.lang.IndexOverflowException|ms.lang.IndexOverflowException]] |- ! scope="row" | Since | 3.0.1 |- ! scope="row" | Restricted |
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Returns the element specified at the index of the array. If the element doesn't exist, an exception is thrown.
=== Vital Info ===
{| style="width: 40%;" cellspacing="1" cellpadding="1" border="1" class="wikitable"
|-
! scope="col" width="20%" |
! scope="col" width="80%" |
|-
! scope="row" | Name
| array_get
|-
! scope="row" | Returns
| mixed
|-
! scope="row" | Usages
| array, index, [default]
|-
! scope="row" | Throws
| [[../objects/ms.lang.CastException|ms.lang.CastException]][[../objects/ms.lang.IndexOverflowException|ms.lang.IndexOverflowException]] |- ! scope="row" | Since | 3.0.1 |- ! scope="row" | Restricted |
No
|-
! scope="row" | Optimizations
| [[../../Optimizer#OPTIMIZE_CONSTANT|OPTIMIZE_CONSTANT]]
|}You can use a more traditional method to access elements in an array: array[index] is the same as array_get(array, index), where array is a variable, or function that is an array. In fact, the compiler converts array[index] into array_get(array, index). So if there is a problem with your code, you will get an error message about a problem with the array_get function, even though you may not be using that function directly. If using the plain function access, then if a default is provided, the function will always return that value if the array otherwise doesn't have a value there. This is opposed to throwing an exception or returning null.
=== Usages ===
array_get(array, index, [default])=== Examples === ====Example 1==== Demonstrates basic usage Given the following code:
msg(array(0, 1, 2)[2]);

1 {{function|msg}}({{function|array}}(0, 1, 2)[2]);
2====Example 2==== Demonstrates exception Given the following code:
msg(array()[1]);

1 {{function|msg}}({{function|array}}()[1]);
(Throws ms.lang.IndexOverflowException: The element at index "1" does not exist)====Example 3==== Demonstrates basic functional usage Given the following code:
msg(array_get(array(1, 2, 3), 2));

1 {{function|msg}}({{function|array_get}}({{function|array}}(1, 2, 3), 2));
3====Example 4==== Demonstrates default (note that you cannot use the bracket syntax with this) Given the following code:
msg(array_get(array(), 1, 'default'));

1 {{function|msg}}({{function|array_get}}({{function|array}}(), 1, 'default'));
default===See Also===
[[API/functions/array_set.html|array_set]]
, [[API/functions/array.html|array]]
, [[Arrays|Learning Trail: Arrays]]Find a bug in this page? Edit this page yourself, then submit a pull request. (Note this page is automatically generated from the documentation in the source code.)