Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an array, hashtable and dictionary?

Tags:

powershell

What is the proper way to create an array, hashtable and dictionary?

$array = [System.Collections.ArrayList]@() 

$array.GetType() returns ArrayList, OK.

$hashtable = [System.Collections.Hashtable] 

$hashtable.GetType() returns RuntimeType, Not OK.

$dictionary = ?  

How to create a dictionary using this .NET way?

What is the difference between dictionary and hashtable? I am not sure when I should use one of them.

like image 240
expirat001 Avatar asked Feb 17 '15 14:02

expirat001


People also ask

How do I create a dictionary in PowerShell?

Creating Ordered Dictionaries You can create an ordered dictionary by adding an object of the OrderedDictionary type, but the easiest way to create an ordered dictionary is use the [ordered] attribute. The [ordered] attribute is introduced in PowerShell 3.0. Place the attribute immediately before the "@" symbol.

Is a Hashtable an array?

A hashtable is a data structure, much like an array, except you store each value (object) using a key.

How do I create a Hashtable in PowerShell?

To create a hash table in PowerShell, you'll use an @ symbol followed by an opening curly brace and a closing curly brace as shown below. Here you can see my hash table is now three lines with a key/value pair in the middle. It can also be represented on one line as well.

How do you create an array in PowerShell?

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( = ). The comma can also be used to initialize a single item array by placing the comma before the single item.


2 Answers

The proper way (i.e. the PowerShell way) is:

Array:

> $a = @() > $a.gettype()  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Object[]                                 System.Array 

Hashtable / Dictionary:

> $h = @{} > $h.gettype()  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Hashtable                                System.Object 

The above should suffice for most dictionary-like scenarios, but if you did explicitly want the type from Systems.Collections.Generic, you could initialise like:

> $d = New-Object 'system.collections.generic.dictionary[string,string]' > $d.gettype()  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Dictionary`2                             System.Object  > $d["foo"] = "bar" > $d | Format-Table -auto  Key   Value ---   ----- foo   bar 
like image 149
arco444 Avatar answered Oct 14 '22 13:10

arco444


If you want to initialize an array you can use the following code:

$array = @()    # empty array $array2 = @('one', 'two', 'three')   # array with 3 values 

If you want to initialize hashtable use the following code:

$hashtable = @{}   # empty hashtable $hashtable2 = @{One='one'; Two='two';Three='three'}   # hashtable with 3 values 

Hashtable and dictionary in Powershell is pretty much the same, so I suggest using hashtable in almost all cases (unless you need to do something in .NET where Dictionary is required)

like image 23
dotnetom Avatar answered Oct 14 '22 14:10

dotnetom