{"id":8581,"date":"2007-02-05T21:14:47","date_gmt":"2007-02-05T21:14:47","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/02\/05\/powershell-tip-how-to-shift-arrays\/"},"modified":"2023-12-11T16:15:30","modified_gmt":"2023-12-12T00:15:30","slug":"powershell-tip-how-to-shift-arrays","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/powershell-tip-how-to-shift-arrays\/","title":{"rendered":"PowerShell Tip: How to \u201cshift\u201d arrays\u2026"},"content":{"rendered":"<p><span style=\"font-family: Consolas; font-size: 10pt;\">It&#8217;s fairly common in scripts to extract the first element out of an array then &#8220;shift&#8221; the remaining elements. Perl has a special shift operator for doing this as do many of the UNIX shells. PowerShell, however, doesn&#8217;t have a shift operator built-in. So how do we shift arrays in PowerShell? Well \u2013 we could do it the brute force way and simply copy all of the elements after the first one but that sounds like too much work. A way to make it a bit easier is to use the range operator (&#8216;..&#8217;) and array slices as shown:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">$first = $oldArray[0]\n<\/span><\/p>\n<p><div class=\"alert alert-danger\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--ErrorBadge\"><\/i><strong>Caution<\/strong><\/p>This behavior can be misleading, if you get down to 1 element left in the array and do `<span style=\"font-family: Courier New; font-size: 10pt;\">$oldArray = $oldArray[1 .. ($oldArray.count-1)]`<\/span> it will not remove the last element<\/div><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">$oldArray = $oldArray[1 .. ($oldArray.count-1)]\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">But that&#8217;s still too much typing. In practice, PowerShell&#8217;s multiple assignment feature is the most effective way to handle this kind of thing. Consider the following array:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (1) &gt; $a = 1,2,3,4,5\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">We can use multiple assignment to split this array into $first and $rest simply by doing:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (2) &gt; $first, $rest = $a\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">The first element from the array is placed into $first, the remaining elements are copies into $rest\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (3) &gt; $first\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">1\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (4) &gt; $rest\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">2\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">3\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">4\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">5\n<\/span><\/p>\n<p><div class=\"alert alert-danger\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--ErrorBadge\"><\/i><strong>Caution<\/strong><\/p>This behavior is outdated, <code>$rest<\/code> will no longer be an array if you do <code>$null,$rest=$rest<\/code>. It will be an System.Int32<\/div><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">If we don&#8217;t care about the first element, then we can use $null instead of $first and the value will simply be discarded.\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (5) &gt; $null, $rest = $rest\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (6) &gt; $rest\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">3\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">4\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">5\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (7) &gt;\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">This is actually a pretty handy approach to solving a variety of list restructuring problems. For example, say we have a list that looks like:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (7) &gt; $list = &#8220;one&#8221;,1,&#8221;two&#8221;,2,&#8221;three&#8221;,3\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">The form of this list is simply a name followed by a value:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (8) &gt; $list\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">one\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">1\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">two\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">2\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">three\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">3\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Now I want to &#8220;fold&#8221; that list into a hashtable as a set of key\/value pairs. Here&#8217;s a one-liner that will do this:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (9) &gt; $hash = @{} ; while ($list) { $key, $value, $list = $list; $hash[$key]=$value }\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">The first two elements of the list go into $key and $value. The remaining elements are assigned back to $list. This loops until $list is empty. The resulting hash table looks like:\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">PS (10) &gt; $hash\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">Name Value\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">&#8212;- &#8212;&#8211;\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">two 2\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">three 3\n<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: 10pt;\">one 1\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">-bruce\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Bruce Payette [MSFT]\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Windows PowerShell Tech Lead\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Visit the Windows PowerShell Team blog at: <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Visit the Windows PowerShell ScriptCenter at: <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a>\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">Windows PowerShell in Action (book): <a href=\"http:\/\/manning.com\/powershell\">http:\/\/manning.com\/powershell<\/a>\n<\/span><\/p>\n<p><span style=\"font-family: Consolas; font-size: 10pt;\">\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s fairly common in scripts to extract the first element out of an array then &#8220;shift&#8221; the remaining elements. Perl has a special shift operator for doing this as do many of the UNIX shells. PowerShell, however, doesn&#8217;t have a shift operator built-in. So how do we shift arrays in PowerShell? Well \u2013 we could [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>It&#8217;s fairly common in scripts to extract the first element out of an array then &#8220;shift&#8221; the remaining elements. Perl has a special shift operator for doing this as do many of the UNIX shells. PowerShell, however, doesn&#8217;t have a shift operator built-in. So how do we shift arrays in PowerShell? Well \u2013 we could [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=8581"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=8581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=8581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=8581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}