Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does powershell have associative arrays?

I am writing a function that returns an id, name pair.

I would like to do something like

$a = get-name-id-pair() $a.Id $a.Name 

like is possible in javascript. Or at least

$a = get-name-id-pair() $a["id"] $a["name"] 

like is possible in php. Can I do that with powershell?

like image 284
George Mauer Avatar asked Oct 01 '09 19:10

George Mauer


People also ask

Does PowerShell have arrays?

PowerShell ArraysArrays in PowerShell can contain one or more items. An item can be a string, an integer, an object, or even another array, and one array can contain any combination of these items. Each of these items has an index, which always starts (sometimes confusingly) at 0.

What are arrays in PowerShell?

An array is a data structure that is designed to store a collection of items. The items can be the same type or different types. Beginning in Windows PowerShell 3.0, a collection of zero or one object has some properties of arrays.

What are differences between an array and hash in PowerShell?

An array, which is sometimes referred to as a collection, stores a list of items. A hash table, which is sometimes called a dictionary or an associative array, stores a paired list of items.

Are all PHP arrays associative?

PHP treats all arrays as associative, so there aren't any built in functions.


2 Answers

also

$a = @{'foo'='bar'} 

or

$a = @{} $a.foo = 'bar' 
like image 133
Doug Finke Avatar answered Oct 02 '22 18:10

Doug Finke


Yes. Use the following syntax to create them

$a = @{} $a["foo"] = "bar" 
like image 44
JaredPar Avatar answered Oct 02 '22 19:10

JaredPar