Content deleted Content added
m link foreach loop using Find link |
moved =PowerShell= up into alphabetic order |
||
Line 976:
% remove something
MyDict /rouge undef</code>
=== [[Windows PowerShell|PowerShell]] ===
Unlike many other [[command line interpreter]]s, PowerShell has built-in, language-level support for defining associative arrays.
For example:
<source lang=PowerShell>
$phonebook = @{
'Sally Smart' = '555-9999';
'John Doe' = '555-1212';
'J. Random Hacker' = '553-1337'
}
</source>
Like in JavaScript, if the property name is a valid identifier, the quotes can be omitted, e.g.:
<source lang=PowerShell>
$myOtherObject = @{ foo = 42; bar = $false }
</source>
Entries can be separated by either a semicolon or a newline, e.g.:
<source lang=Text>
$myOtherObject = @{ foo = 42
bar = $false ;
zaz = 3
}
</source>
Keys and values can be any [[.NET Framework|.NET]] object type, e.g.:
<source lang=PowerShell>
$now = [DateTime]::Now
$tomorrow = $now.AddDays(1)
$ProcessDeletionSchedule = @{
(Get-Process notepad) = $now
(Get-Process calc) = $tomorrow
}
</source>
It is also possible to create an empty associative array and add single entries or even other associative arrays to it later on.
<source lang=PowerShell>
$phonebook = @{}
$phonebook += @{ 'Sally Smart' = '555-9999' }
$phonebook += @{ 'John Doe' = '555-1212'; 'J. Random Hacker' = '553-1337' }
</source>
New entries can also be added by using the array index operator, the property operator or the <code>Add()</code> method of the underlying .NET object:
<source lang=PowerShell>
$phonebook = @{}
$phonebook['Sally Smart'] = '555-9999'
$phonebook.'John Doe' = '555-1212'
$phonebook.Add('J. Random Hacker', '553-1337')
</source>
To dereference assigned objects the array index operator, the property operator or the parameterized property <code>Item()</code> of the .NET object can be used:
<source lang=PowerShell>
$phonebook['Sally Smart']
$phonebook.'John Doe'
$phonebook.Item('J. Random Hacker')
</source>
You can loop through an associative array as follows:
<source lang=PowerShell>
$phonebook.Keys | foreach { "Number for {0}: {1}" -f $_,$phonebook.$_ }
</source>
An entry can be removed using the <code>Remove()</code> method of the underlying .NET object:
<source lang=PowerShell>
$phonebook.Remove('Sally Smart')
</source>
Hash tables can be added, e.g.:
<source lang=PowerShell>
$hash1 = @{ a=1; b=2 }
$hash2 = @{ c=3; d=4 }
$hash3 = $hash1 + $hash2
</source>
=== [[Python (programming language)|Python]] ===
Line 1,307 ⟶ 1,391:
MessageBox.Show(entry.Key & " = " & entry.Value)
Next
</source>
|