Powershell: Unable to create an array containing a single empty array

Created on 7 Mar 2018  路  5Comments  路  Source: PowerShell/PowerShell

When trying to create an array containing a single empty array I am left with only a single empty array:

Steps to reproduce

$arr = @(@())
$arr.Count
$arr[0] += "something"
$arr

Expected behavior

1
something

Actual behavior

0
Index was outside the bounds of the array.
At line:1 char:1
+ $arr[0] +=  "something"
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException

Environment data

> $PSVersionTable
Name                           Value                                                                                                              
----                           -----                                                                                                              
PSVersion                      6.0.1                                                                                                              
PSEdition                      Core                                                                                                               
GitCommitId                    v6.0.1                                                                                                             
OS                             Linux 4.15.6-1-ARCH #1 SMP PREEMPT Sun Feb 25 12:53:23 UTC 2018                                                    
Platform                       Unix                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                            
PSRemotingProtocolVersion      2.3                                                                                                                
SerializationVersion           1.1.0.1                                                                                                            
WSManStackVersion              3.0
Issue-Question Resolution-Answered

Most helpful comment

To complement what @BrucePay is saying:

@(...) is _not_ an array _constructor_ (it's an array _guarantor_, if you will, as Bruce explains).

All you need to construct arrays from explicitly enumerated elements is the array-construction operator, , - e.g.:

'one', 'two'  # array "literal" with 2 elements; you do NOT need @('one', 'two')

However, the belief that @(...) is necessary in this scenario is very widespread and probably impossible to dispel at this point (prior to v5.1, use of @(...) even incurred a performance penalty in this scenario).

This is not helped by the fact that @() is indeed the easiest way to create an _empty_ array.

All 5 comments

I've found a work around for this:

$arr = @(,@())

will create an array containing a single empty array.

Creating an array containing 2 empty arrays works just fine:

$arr = @(@(),@())
$arr.Count # returns 2
$arr[0] += "Hello"
$arr[0] += "World"
$arr[1] += "Salut"
$arr[1] += "Mondo"
#$arr = @(@("Hello", "World"),@("Salut", "Mondo"))

Just a quick question how can I get an installer of the nightly version of powershell 6.1 I have been looking everywhere on the site cannot seem to locate. I am a 16 year old high school student whom is looking at and wanting to code programs and apps. Please help

@frogamic:

The behavior is as designed: loosely speaking, @(...) means: treat output from the statement(s) within as an array, _unless it already is_.

Thus, @() outputs an empty array, and applying _another_ @() just _passes that empty array through_.

An alternative way to achieve what you want would be:

$arr = , @()  # construct a 1-element array whose only element is the empty array.

Similarly, creating an array with 2 empty sub-arrays can be simplified to:

$arr = @(), @()

Hi @frogamic, the @( ... ) operator guarantees that the result produced by the code inside is always an array. This operator semantic was introduced because of the way streaming works. If the pipeline returns a single element, you don't know if it's supposed to be a scalar value or an array of one element. @( ... ) guarantees that you will always get an array even if the pipeline returns 0 or 1 elements. This way you don't have to special-case your code to handle scalar and non-scalar values.

To complement what @BrucePay is saying:

@(...) is _not_ an array _constructor_ (it's an array _guarantor_, if you will, as Bruce explains).

All you need to construct arrays from explicitly enumerated elements is the array-construction operator, , - e.g.:

'one', 'two'  # array "literal" with 2 elements; you do NOT need @('one', 'two')

However, the belief that @(...) is necessary in this scenario is very widespread and probably impossible to dispel at this point (prior to v5.1, use of @(...) even incurred a performance penalty in this scenario).

This is not helped by the fact that @() is indeed the easiest way to create an _empty_ array.

Was this page helpful?
0 / 5 - 0 ratings