76211

How do I create an empty array of arrays in Powershell?

Question:

I want to create an empty array of arrays in Powershell to hold "tuples" of values (arrays are immutable).

Therefore I try something like:

The type of $arr is Object[]. I've read that += @(1,2) appends the given element (i.e. @(1,2)) to $arr (actually creates a new array). However, in this case it seems that the arrays are concatenated, why?

$arr = @() $arr += @(1,2) $arr.Length // 2 (not 1)

If I do as follows, it seems that $arr contains the two arrays @(1,2),@(3,4), which is what I want:

$arr = @() $arr += @(1,2),@(3,4) $arr.Length // 2

How do I initialize an empty array of arrays, such that I can add one subarray at a time, like $arr += @(1,2)?

Answer1:

The + operator concatenates arrays. To add an array as a single element, prefix the element to add with a comma. Something like @() + , (1,2) + , (3, 4).

Answer2:

The answer from Bruce Payette will work. The syntax seems a bit awkward to me, but it does work. At least it is not Perl.

Another way to do this would be with an ArrayList. To me, this syntax is more clear and more likely to be understood by another developer (or myself) in six months.

[System.Collections.ArrayList]$al = @() $al.Add(@(1,2)) $al.Add(@(3,4)) foreach ($e in $al) { $e $e.GetType() }

Answer3:

As far as I can tell, you can't do it natively in PowerShell, nor can you do it with the [System.Array] type. In both cases, you seem to need to define both the length and the type of the array. Because I wanted a completely empty array of arrays, with the ability to store any type of value in the array, I did it this way.

$x=[System.Collections.ArrayList]::new() $x.Count $x.GetType() $x.Add([System.Collections.ArrayList]::new()) > $null $x.Count $x[0].Count $x[0].GetType() $x[0].Add("first element") > $null $x.Count $x[0].Count $x[0][0]

Recommend

  • Finding right triangle coordinates in binary array
  • How to make Numpy treat each row/tensor as a value
  • How do you make a view drop dimensions?
  • Converting a Bidimensional Array (Numbers) Into A Dimensional Array and Viceversa on Java
  • Showing a concatenated string from multiple values of observable array
  • Extracting infromation from multiple JSON files to single CSV file in python
  • Plot a CSV file where the delimiter is '; ' (semicolon + space)
  • concatenating select menus into a single form input
  • How to remove comma or any characters from Python dataframe column name
  • Creating JS objects in PHP with commas in between
  • Using an STL Iterator without initialising it
  • Unix Network Programming Clarification
  • How to concat Pandas dataframe columns
  • Google map in top corner left
  • What Makes These Two Array Adds Different?
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Bigquery event streaming and table creation
  • Cannot upload to OneDrive using the new SDK
  • converting text file into xml using php?
  • Android activity accessing service's static reference before the service is ready
  • Change Inet root folder for iis 7
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How to make Safari send if-modified-since header?
  • Jquery - Jquery Wysiwyg return html as a string
  • How to pass list parameters for each object using Spring MVC?
  • Arrays break string types in Julia
  • retrieve vertices with no linked edge in arangodb
  • WPF Applying a trigger on binding failure
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • JaxB to read class hierarchy
  • UserPrincipal.Current returns apppool on IIS