Skip to content

Commit f4f8176

Browse files
Merge pull request #300082 from mumian/0519-array-caret
Document array[^index]
2 parents d30d7c1 + 6451ef0 commit f4f8176

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

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

Lines changed: 54 additions & 36 deletions
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: 05/09/2025
5+
ms.date: 05/20/2025
66
ms.custom: devx-track-bicep
77
---
88

@@ -12,7 +12,11 @@ This article describes the data types that are supported in [Bicep](./overview.m
1212

1313
## Arrays
1414

15-
Arrays start with a left bracket (`[`) and end with a right bracket (`]`). In Bicep, you can declare an array in a single line or in multiple lines. Commas (`,`) are used between values in single-line declarations, but they aren't used in multiple-line declarations. You can mix and match single-line and multiple-line declarations. The multiple-line declaration requires [Bicep CLI](./install.md#visual-studio-code-and-bicep-extension) version 0.7.X or later.
15+
A **array** in Bicep is an ordered collection of values—such as strings, integers, objects, or even other arrays—commonly used to group related items like resource names, configuration settings, or parameters. Arrays are helpful for organizing deployment data, passing lists to resources, and iterating over multiple values.
16+
17+
Arrays in Bicep are immutable. Once declared, their contents can't be changed. To "modify" an array, create a new array using functions like [`concat`](./bicep-functions-array.md#concat), [`map`](./bicep-functions-lambda.md#map), or [`filter`](./bicep-functions-lambda.md#filter).
18+
19+
You can declare arrays in Bicep using either single-line or multi-line syntax. Multi-line array declarations require [Bicep CLI version 0.7.X or later](https://github.com/Azure/bicep/releases/tag/v0.7.4).
1620

1721
```bicep
1822
var multiLineArray = [
@@ -27,9 +31,9 @@ var mixedArray = ['abc', 'def'
2731
'ghi']
2832
```
2933

30-
Each array element can be of any type. You can have an array where each item is the same data type, or an array that holds different data types.
34+
**Single-line arrays** use commas (`,`) to separate values. **Multi-line arrays** don't use commas between values. You can mix single-line and multi-line declarations as needed.
3135

32-
The following example shows an array of integers and an array of different types.
36+
Each array element can be of any type. You can have an array where each item is the same data type, or an array that holds different data types.
3337

3438
```bicep
3539
var integerArray = [
@@ -44,27 +48,40 @@ var mixedArray = [
4448
true
4549
'example string'
4650
]
51+
52+
var arrayOfObjects = [
53+
{ name: 'dev', size: 1 }
54+
{ name: 'prod', size: 2 }
55+
]
4756
```
4857

49-
Arrays in Bicep are based on zero. In the following example, the expression `exampleArray[0]` evaluates to 1 and `exampleArray[2]` evaluates to 3. The index of the indexer might be another expression. The expression `exampleArray[index]` evaluates to 2. Integer indexers are only allowed on the expression of array types.
58+
Arrays in Bicep are zero-based. You can access elements by index:
5059

5160
```bicep
61+
var exampleArray = [1, 2, 3]
62+
output firstElement int = exampleArray[0] // 1
63+
output thirdElement int = exampleArray[2] // 3
64+
5265
var index = 1
66+
output secondElement int = exampleArray[index] // 2
67+
```
5368

54-
var exampleArray = [
55-
1
56-
2
57-
3
58-
]
69+
Starting with [Bicep CLI version 0.34.x](https://github.com/Azure/bicep/releases/tag/v0.34.1), you can use the `array[^index]` syntax to access elements from the end of an array — `^1` refers to the last element, `^2` to the second-to-last, and so on.
70+
71+
```bicep
72+
var exampleArray = [1, 2, 3]
73+
74+
output lastElement int = exampleArray[^1] // 3
75+
output secondToLastElement int = exampleArray[^2] // 2
5976
```
6077

61-
You get the following error when the index is out of bounds:
78+
If you access an index that is out of bounds, you get an error:
6279

6380
```error
6481
The language expression property array index 'x' is out of bounds
6582
```
6683

67-
To avoid this exception, use the [Or logical operator](./operators-logical.md#or-), as shown in the following example:
84+
To avoid out-of-bounds exception, use the [Or logical operator](./operators-logical.md#or-), as shown in the following example:
6885

6986
```bicep
7087
param emptyArray array = []
@@ -76,15 +93,15 @@ output bar bool = length(numberArray) <= 3 || numberArray[3] == 4
7693

7794
### Array-related operators
7895

79-
* Use [Comparison operators](./operators-comparison.md) to compare two arrays.
80-
* Use [Index accessor](./operators-access.md#index-accessor) to get an element from an array.
81-
* Use [Safe-dereference operator](./operator-safe-dereference.md) to access elements of an array.
82-
* Use [Spread](./operator-spread.md) to merge arrays.
96+
- Use [Comparison operators](./operators-comparison.md) to compare two arrays.
97+
- Use [Index accessor](./operators-access.md#index-accessor) to get an element from an array.
98+
- Use [Safe-dereference operator](./operator-safe-dereference.md) to access elements of an array.
99+
- Use [Spread](./operator-spread.md) to merge arrays.
83100

84101
### Array-related functions
85102

86-
* See [Array functions](./bicep-functions-array.md).
87-
* See [Lambda functions](./bicep-functions-lambda.md).
103+
- See [Array functions](./bicep-functions-array.md).
104+
- See [Lambda functions](./bicep-functions-lambda.md).
88105

89106
## Booleans
90107

@@ -94,12 +111,12 @@ When you specify Boolean values, use `true` or `false`. Don't surround the value
94111
param exampleBool bool = true
95112
```
96113

97-
## Boolean-related operators
114+
### Boolean-related operators
98115

99-
* Use [Comparison operators](./operators-comparison.md) to compare boolean values.
100-
* See [Logical operators](./operators-logical.md).
116+
- Use [Comparison operators](./operators-comparison.md) to compare boolean values.
117+
- See [Logical operators](./operators-logical.md).
101118

102-
## Boolean-related functions
119+
### Boolean-related functions
103120

104121
See [Logical function](./bicep-functions-logical.md)
105122

@@ -140,8 +157,8 @@ Floating point, decimal, or binary formats aren't currently supported.
140157

141158
### Integer-related operators
142159

143-
* See [Comparison operators](./operators-comparison.md).
144-
* See [Numeric operators](./operators-numeric.md).
160+
- See [Comparison operators](./operators-comparison.md).
161+
- See [Numeric operators](./operators-numeric.md).
145162

146163
### Integer-related functions
147164

@@ -237,10 +254,10 @@ output bar bool = contains(objectToTest, 'four') && objectToTest.four == 4
237254

238255
### Object-related operators
239256

240-
* Use [Comparison operators](./operators-comparison.md) to compare objects.
241-
* Use [Index accessor](./operators-access.md#index-accessor) to get a property from an object.
242-
* Use [Safe-dereference operator](./operator-safe-dereference.md) to access object members.
243-
* Use [Spread](./operator-spread.md) to merge objects.
257+
- Use [Comparison operators](./operators-comparison.md) to compare objects.
258+
- Use [Index accessor](./operators-access.md#index-accessor) to get a property from an object.
259+
- Use [Safe-dereference operator](./operator-safe-dereference.md) to access object members.
260+
- Use [Spread](./operator-spread.md) to merge objects.
244261

245262
### Object-related functions
246263

@@ -348,10 +365,11 @@ is ${blocked}'''
348365

349366
### String-related operators
350367

351-
* See [Comparison operators](./operators-comparison.md).
368+
- See [Comparison operators](./operators-comparison.md).
352369

353370
### String-related functions
354371

372+
- See [String functions](./bicep-functions-string.md).
355373

356374
## Union types
357375

@@ -408,14 +426,14 @@ The parameter value is validated based on the discriminated property value. For
408426

409427
The union type has some limitations:
410428

411-
- Union types must be reducible to a single Azure Resource Manager type. The following definition is invalid:
412-
413-
```bicep
414-
type foo = 'a' | 1
415-
```
429+
- Union types must be reducible to a single Azure Resource Manager type. The following definition is invalid:
430+
431+
```bicep
432+
type foo = 'a' | 1
433+
```
416434

417-
- Only literals are permitted as members.
418-
- All literals must be of the same primitive data type (for example, all strings or all integers).
435+
- Only literals are permitted as members.
436+
- All literals must be of the same primitive data type (for example, all strings or all integers).
419437

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

articles/azure-resource-manager/bicep/operators-access.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep accessor operators
33
description: Describes Bicep resource access operator and property access operator.
44
ms.topic: reference
55
ms.custom: devx-track-bicep
6-
ms.date: 02/12/2025
6+
ms.date: 05/20/2025
77
---
88

99
# Bicep accessor operators
@@ -13,6 +13,7 @@ The accessor operators are used to access child resources, properties on objects
1313
| Operator | Name |
1414
| ---- | ---- |
1515
| `[]` | [Index accessor](#index-accessor) |
16+
| `[^index]` | [Reverse index accessor](#reverse-index-accessor) |
1617
| `.` | [Function accessor](#function-accessor) |
1718
| `::` | [Nested resource accessor](#nested-resource-accessor) |
1819
| `.` | [Property accessor](#property-accessor) |
@@ -68,6 +69,35 @@ Output from the example:
6869
| ---- | ---- | ---- |
6970
| accessorResult | string | 'Development' |
7071

72+
## Reverse index accessor
73+
74+
Beginning with [Bicep CLI version 0.34.x](https://github.com/Azure/bicep/releases/tag/v0.34.1), the reverse index accessor operator (`^`) allows you to retrieve an element from an array by counting from the end. This one-based index means `^1` returns the last item, `^2` the second-to-last, and so on. The index must be a positive integer greater than zero and can be specified as a literal or an expression that evaluates to an integer.
75+
76+
`array[^index]`
77+
78+
If the index exceeds the length of the array, a compilation error occurs for static indices, or a runtime error occurs for dynamic indices.
79+
80+
For constant arrays, the operator is evaluated at compile time. For dynamic inputs, such as [parameters](./parameters.md), evaluation occurs at deployment time.
81+
82+
### Example
83+
84+
```bicep
85+
var items = [
86+
'apple'
87+
'banana'
88+
'orange'
89+
'grape'
90+
]
91+
92+
output secondToLast string = items[^2]
93+
```
94+
95+
Output from the example:
96+
97+
| Name | Type | Value |
98+
| ---- | ---- | ---- |
99+
| secondToLast | string | 'orange' |
100+
71101
## Function accessor
72102

73103
`resourceName.functionName()`

articles/azure-resource-manager/bicep/operators.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep operators
33
description: Describes the Bicep operators available for Azure Resource Manager deployments.
44
ms.topic: reference
55
ms.custom: devx-track-bicep, devx-track-arm-template
6-
ms.date: 02/12/2025
6+
ms.date: 05/20/2025
77
---
88

99
# Bicep operators
@@ -46,6 +46,7 @@ The accessor operators are used to access nested resources and properties on obj
4646
| Operator | Name | Description |
4747
| ---- | ---- | ---- |
4848
| `[]` | [Index accessor](./operators-access.md#index-accessor) | Access an element of an array or property on an object. |
49+
| `[^index]` | [Reverse index accessor](./operators-access.md#reverse-index-accessor) | Accesses an array element by index, counting from the end of the array (1-based from the end). |
4950
| `.` | [Function accessor](./operators-access.md#function-accessor) | Call a function on a resource. |
5051
| `::` | [Nested resource accessor](./operators-access.md#nested-resource-accessor) | Access a nested resource from outside of the parent resource. |
5152
| `.` | [Property accessor](./operators-access.md#property-accessor) | Access properties of an object. |

0 commit comments

Comments
 (0)