I am learning PowerShell. I would like to know how to declare the hash table in PowerShell? Also what is the actual usage of hash table? What are are the benefits of it?
I am learning PowerShell. I would like to know how to declare the hash table in PowerShell? Also what is the actual usage of hash table? What are are the benefits of it?
PowerShell Hash Table allows you to store key-value pairs. It is an array that allows you to store data in a “key-value” pair association. The “key” and “value” entries can be any data type and length. The elements must be quoted if they contain a space.
Just like an array, a hash table is declared with "@" symbol. The hash “key-value” pairs are enclosed in curly brackets {} and its elements are separated by the semi-colon.
To create a hash table:
$EmpName = @{"Alap"; "Sanjay"; "Arpita"}
Source: powershellpro
A hash table is simply a collection of name-value pairs, very much like the FileSystemObject’s Dictionary object. For example, suppose you have a collection of Indian states and their capitals. Each state has one and only one capital and a given city can be the capital of only one state. So a hash table goes something like this:
$states = @{"Maharashtra" = "Mumbai"; "Rajasthan" = "Jaipur"; "Andhra Pradesh" = "Hyderabad"}
So the syntax for creating a hash table involves using an at the rate sign (@) followed by a pair of curly braces.
A hash table consists of two parts: an array and a mapping function, known as a hash function. The hash function is a mapping from the input space to the integer space that defines the indices of the array. In other words, the hash function provides a way for assigning numbers to the input data such that the data can then be stored at the array index corresponding to the assigned number. A hash function doesn't guarantee that every input will map to a different output. There is always the chance that two inputs will hash to the same output. This indicates that both elements should be inserted at the same place in the array, and this is impossible.
Source: sparknotes
Bookmarks