Skip to content

Commit ac2d8f1

Browse files
Merge pull request #302331 from mumian/0707-indexfromend
ARM functions indexFromEnd
2 parents 80187c8 + b1f9f0e commit ac2d8f1

File tree

4 files changed

+156
-4
lines changed

4 files changed

+156
-4
lines changed

articles/azure-resource-manager/bicep/data-types.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Data types in Bicep
33
description: This article describes the data types that are available in Bicep.
44
ms.topic: reference
5-
ms.date: 06/30/2025
5+
ms.date: 07/07/2025
66
ms.custom: devx-track-bicep
77
---
88

@@ -437,6 +437,16 @@ The union type has some limitations:
437437

438438
You can use the union type syntax in [user-defined data types](./user-defined-data-types.md).
439439

440+
## Nullable types
441+
442+
You can make any primitive or complex type nullable by appending a `?` to the type name. This allows the parameter, variable, or output to accept null as a valid value. For example:
443+
444+
```bicep
445+
output description string? = null
446+
output config object? = null
447+
output optionalValue int? = null
448+
```
449+
440450
## Secure strings and objects
441451

442452
Secure strings use the same format as string, and secure objects use the same format as object. With Bicep, you add the `@secure()` [decorator](./parameters.md#use-decorators) to a string or object.

articles/azure-resource-manager/templates/template-functions-array.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Template functions - arrays
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) for working with arrays.
44
ms.topic: reference
55
ms.custom: devx-track-arm-template
6-
ms.date: 02/12/2025
6+
ms.date: 07/07/2025
77
---
88

99
# Array functions for ARM templates
@@ -223,6 +223,57 @@ The output from the preceding example with the default values is:
223223
| arrayOutput | String | one |
224224
| stringOutput | String | O |
225225

226+
## indexFromEnd
227+
228+
`indexFromEnd(sourceArray, reverseIndex)`
229+
230+
Returns an element of the array by counting backwards from the end. This is useful when you want to reference elements starting from the end of a list, rather than the beginning. The [`tryIndexFromEnd`](#tryindexfromend) function is a safe version of `indexFromEnd`.
231+
232+
In Bicep, use the [Reserved index accessor](../bicep/operators-access.md#reverse-index-accessor) operator.
233+
234+
### Parameters
235+
236+
| Parameter | Required | Type | Description |
237+
|:--- |:--- |:--- |:--- |
238+
| sourceArray |Yes |array |The value to retrieve the element by counting backwards from the end. |
239+
| reverseIndex |Yes |integer |The one-based index from the end of the array. |
240+
241+
### Return value
242+
243+
A single element from an array, selected by counting backward from the end of the array.
244+
245+
### Example
246+
247+
The following example shows how to use the `indexFromEnd` function.
248+
249+
```json
250+
{
251+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
252+
"contentVersion": "1.0.0.0",
253+
"variables": {
254+
"items": [
255+
"apple",
256+
"banana",
257+
"orange",
258+
"grape"
259+
]
260+
},
261+
"resources": [],
262+
"outputs": {
263+
"secondToLast": {
264+
"type": "string",
265+
"value": "[indexFromEnd(variables('items'), 2)]"
266+
}
267+
}
268+
}
269+
```
270+
271+
The output from the preceding example with the default values is:
272+
273+
| Name | Type | Value |
274+
| ---- | ---- | ----- |
275+
| secondToLast | String | orange |
276+
226277
## indexOf
227278

228279
`indexOf(arrayToSearch, itemToFind)`
@@ -693,6 +744,92 @@ The output from the preceding example with the default values is:
693744
| arrayOutput | Array | ["one", "two"] |
694745
| stringOutput | String | on |
695746

747+
## tryIndexFromEnd
748+
749+
`tryndexFromEnd(sourceArray, reverseIndex)`
750+
751+
The `tryIndexFromEnd` function is a safe version of [`indexFromEnd`](#indexfromend). It retrieves a value from an array by counting backward from the end without throwing an error if the index is out of range.
752+
753+
In Bicep, use the [Reserved index accessor](../bicep/operators-access.md#reverse-index-accessor) operator and the [Safe dereference](../bicep/operator-safe-dereference.md#safe-dereference) operator.
754+
755+
### Parameters
756+
757+
| Parameter | Required | Type | Description |
758+
|:--- |:--- |:--- |:--- |
759+
| sourceArray |Yes |array |The value to retrieve the element by counting backwards from the end. |
760+
| reverseIndex |Yes |integer |The one-based index from the end of the array. |
761+
762+
### Return value
763+
764+
If the index is valid (within array bounds), returns the array element at that reverse index. If the index is out of range, returns null instead of throwing an error.
765+
766+
### Example
767+
768+
The following example shows how to use the `tryIndexFromEnd` function:
769+
770+
```json
771+
{
772+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
773+
"contentVersion": "1.0.0.0",
774+
"variables": {
775+
"items": [
776+
"apple",
777+
"banana",
778+
"orange",
779+
"grape"
780+
]
781+
},
782+
"resources": [],
783+
"outputs": {
784+
"secondToLast": {
785+
"type": "string",
786+
"value": "[tryIndexFromEnd(variables('items'), 2)]"
787+
}
788+
}
789+
}
790+
```
791+
792+
The output from the preceding example with the default values is:
793+
794+
| Name | Type | Value |
795+
| ---- | ---- | ----- |
796+
| secondToLast | String | orange |
797+
798+
The following example shows an out-of-bound scenario:
799+
800+
```json
801+
{
802+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
803+
"languageVersion": "2.0",
804+
"contentVersion": "1.0.0.0",
805+
"parameters": {
806+
"items": {
807+
"type": "array",
808+
"defaultValue": [
809+
"apple",
810+
"banana",
811+
"orange",
812+
"grape"
813+
]
814+
}
815+
},
816+
"resources": {},
817+
"outputs": {
818+
"outOfBound": {
819+
"type": "string",
820+
"nullable": true,
821+
"value": "[tryIndexFromEnd(parameters('items'), 5)]"
822+
}
823+
}
824+
}
825+
```
826+
827+
The output from the preceding example with the default values is:
828+
829+
| Name | Type | Value |
830+
| ---- | ---- | ----- |
831+
| outOfBound | String | (null) |
832+
696833
## union
697834

698835
`union(arg1, arg2, arg3, ...)`

articles/azure-resource-manager/templates/template-functions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Template functions
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) to retrieve values, work with strings and numerics, and retrieve deployment information.
44
ms.topic: reference
55
ms.custom: devx-track-arm-template
6-
ms.date: 02/12/2025
6+
ms.date: 07/07/2025
77
---
88

99
# ARM template functions
@@ -29,6 +29,7 @@ The [any function](../bicep/bicep-functions-any.md) is available in Bicep to hel
2929
<a id="createarray" aria-hidden="true"></a>
3030
<a id="empty" aria-hidden="true"></a>
3131
<a id="first" aria-hidden="true"></a>
32+
<a id="indexfromend" aria-hidden="true"></a>
3233
<a id="indexof" aria-hidden="true"></a>
3334
<a id="intersection" aria-hidden="true"></a>
3435
<a id="last" aria-hidden="true"></a>
@@ -39,6 +40,7 @@ The [any function](../bicep/bicep-functions-any.md) is available in Bicep to hel
3940
<a id="range" aria-hidden="true"></a>
4041
<a id="skip" aria-hidden="true"></a>
4142
<a id="take" aria-hidden="true"></a>
43+
<a id="tryindexfromend" aria-hidden="true"></a>
4244
<a id="union" aria-hidden="true"></a>
4345

4446
## Array functions
@@ -51,6 +53,7 @@ Resource Manager provides several functions for working with arrays.
5153
* [createArray](template-functions-array.md#createarray)
5254
* [empty](template-functions-array.md#empty)
5355
* [first](template-functions-array.md#first)
56+
* [indexFromEnd](template-functions-array.md#indexfromend)
5457
* [indexOf](template-functions-array.md#indexof)
5558
* [intersection](template-functions-array.md#intersection)
5659
* [last](template-functions-array.md#last)
@@ -61,6 +64,7 @@ Resource Manager provides several functions for working with arrays.
6164
* [range](template-functions-array.md#range)
6265
* [skip](template-functions-array.md#skip)
6366
* [take](template-functions-array.md#take)
67+
* [tryIndexFromEnd](template-functions-array.md#tryindexfromend)
6468
* [union](template-functions-array.md#union)
6569

6670
For Bicep files, use the [array](../bicep/bicep-functions-array.md) functions.

articles/azure-resource-manager/templates/toc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ items:
293293
- name: Template best practices
294294
href: best-practices.md
295295
- name: Data types
296+
displayName: nullable types,union type,secure strings,secure objects,data type assignability
296297
href: data-types.md
297298
- name: Frequently asked questions
298299
href: frequently-asked-questions.yml
@@ -469,7 +470,7 @@ items:
469470
- name: All functions
470471
href: template-functions.md
471472
- name: Array functions
472-
displayName: array,concat,contains,createArray,empty,first,indexOf,intersection,last,lastIndexOf,length,max,min,range,skip,take,union
473+
displayName: array,concat,contains,createArray,empty,first,indexFromEnd,indexOf,intersection,last,lastIndexOf,length,max,min,range,skip,take,tryIndexFromEdn,union
473474
href: template-functions-array.md
474475
- name: CIDR functions
475476
displayName: parseCidr,cidrSubnet,cidrHost

0 commit comments

Comments
 (0)