Search notes:

PowerShell: Iterate over array that contains name/value pairs

In PowerShell, when an array is assigned to an array with fewer elements ($e1, $e2, $rest = $a, $b, $c, $d…), the values of $e… are assigned from the left ($e1 = $a, $e2 = $b etc.) and the last element $rest is assigned the remaining values as an array. This feature can be used to iterate over an array that contains name/value pairs, as showh in the following simple example script:
$name_value_pairs = 'text', 'hello World', 'number', 42, 'fruit', 'apple'

while ($name_value_pairs) {
   $name, $value, $name_value_pairs = $name_value_pairs
   write-host('{0,-7}: {1}' -f $name, $value)
}
Github repository about-PowerShell, path: /language/type/array/iterate-over-name-value-pairs.ps1
When executed, this script prints
text   : hello World
number : 42
fruit  : apple

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1758199462, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/type/array/iterate-over-name-value-pairs(47): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78