{"id":747,"date":"2014-09-02T00:01:00","date_gmt":"2014-09-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/02\/using-a-tuple-in-powershell\/"},"modified":"2022-06-20T13:54:18","modified_gmt":"2022-06-20T20:54:18","slug":"using-a-tuple-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-a-tuple-in-powershell\/","title":{"rendered":"Using a Tuple in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Learn how to use a Tuple in Windows PowerShell<\/span><\/p>\n<p>Hey, Scripting Guy! I read your article from yesterday, so I know about Tuples. How do I actually use one?<\/p>\n<p>\u2014HD<\/p>\n<p>Hello HD,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I just got back from working out with my trainer. I am not nearly as sore today, but then there is always tomorrow. Anyway, I decided to drink a protein shake and a glass of water now. I will have tea later with my mid-morning snack. I grabbed my Surface Pro\u00a03, and I am quickly checking my email to <a href=\"&#109;&#97;&#x69;&#x6c;&#116;&#111;&#x3a;&#x73;c&#114;&#105;&#x70;&#x74;&#101;&#114;&#x40;&#x6d;&#105;&#99;&#x72;&#x6f;s&#111;&#102;&#x74;&#x2e;&#99;&#111;&#x6d;\">&#x73;c&#114;&#105;&#x70;&#x74;&#101;&#114;&#x40;&#x6d;&#105;&#99;&#x72;&#x6f;s&#111;&#102;&#x74;&#x2e;&#99;&#111;&#x6d;<\/a>.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note<\/b>\u00a0 For a good introduction to Tuples, read <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/whats-a-tuple-and-why-would-i-want-to-use-it\/\" target=\"_blank\" rel=\"noopener\">What\u2019s a Tuple, and Why Would I Want to Use It?<\/a>\n<\/p>\n<h2>Creating Tuples<\/h2>\n<p>A two element Tuple is called a 2-tuple, or a pair*. <em>The way I create it is to use the <strong>Create<\/strong><\/em> <em>static method from the System.Tuple .NET Framework class, and specify two elements. Here is an example of creating a pair<\/em>:*<\/p>\n<p style=\"margin-left:30px\">\n  $P = [System.Tuple]::Create(&#8220;Flintstone&#8221;,&#8221;Rubble&#8221;)\n<\/p>\n<p>To access the two elements, I use parameter replacement and the <strong>Format<\/strong> (<strong>-f<\/strong>) operator. I specify each element (beginning at 0) with a pair of curly brackets. On the other side of the format operator, I specify the tuple element (item number).<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> For parameter replacement and the <b>Format<\/b> operator, the elements are zero-based. Therefore, the first element is {0}. On the other hand, when tuples are referenced by item number, they are one-based. Therefore, the first tuple element is <b>Item1<\/b>.\u00a0\n<\/p>\n<p>To use the tuple, I use parameter replacement and the <strong>Format<\/strong> operator. Here is an example:<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Fred&#8217;s last name is {0} and Barney&#8217;s last name is {1}&#8221; -f\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0 $p.Item1, $p.Item2\n<\/p>\n<p>The image that follows illustrates creating a 2-tuple (pair) and accessing each of the elements.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-01.png\" alt=\"Image of command output\" width=\"500\" height=\"325\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to create a 3-tuple (a triple), it works exactly the same way as creating a 2-tuple. All I need to do is to add an additional element. I access the third element in the same way as I access the other elements. Here is the code:<\/p>\n<p style=\"margin-left:30px\">\n  $P = [System.Tuple]::Create(&#8220;Flintstone&#8221;,&#8221;Rubble&#8221;,&#8221;Dino&#8221;)\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Fred&#8217;s last name is {0} and Barney&#8217;s last name is {1}\n<\/p>\n<p style=\"margin-left:30px\">\n  Fred&#8217;s dog is really a dinosaur named {2}&#8221; -f\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0 $p.Item1, $p.Item2, $p.Item3\n<\/p>\n<p>Here is an example of running the script, and the output produced by the script:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can create a tuple with more than eight elements by using the <strong>Rest*** <em>property. I do this by creating a nested tuple in the eighth element. When I reference the elements stored in the **Rest<\/em><\/strong> <em>property, I do so with a double type of attribution: $T.Rest.Item1.Item3. My new tuple is stored within Item1 of the <strong>Rest<\/strong><\/em> *property. Here is an example of how to do this:<\/p>\n<p style=\"margin-left:30px\">\n  $T = [System.Tuple]::Create(&#8220;Betelgeuse&#8221;,&#8221;RW Cephei&#8221;,&#8221;KW Sagitarii&#8221;,&#8221;KY Cygni&#8221;,&#8221;V354 Cephei&#8221;,\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;WOH G64&#8243;,&#8221;Mu Cephei&#8221;,[System.Tuple]::Create(&#8220;V838 Monocerotis&#8221;,&#8221;VV Cephei A&#8221;,\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;VY Canis Majoris&#8221;))\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;The smallest of the top ten stars is {0}.\n<\/p>\n<p style=\"margin-left:30px\">\n  The largest of the top ten stars is {1}&#8221; -f $T.Item1, $T.Rest.item1.Item3\n<\/p>\n<p>The script and output from the script are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>HD, that is all there is to using tuples with Windows PowerShell. Join me tomorrow when I will have a guest blogger, Windows PowerShell MVP, Joel Bennett, talk about tuples and their differences from other data structures.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size:12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use a Tuple in Windows PowerShell Hey, Scripting Guy! I read your article from yesterday, so I know about Tuples. How do I actually use one? \u2014HD Hello HD, Microsoft Scripting Guy, Ed Wilson, is here. I just got back from working out with my trainer. I am not nearly as [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[66,3,4,45],"class_list":["post-747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-net-framework","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use a Tuple in Windows PowerShell Hey, Scripting Guy! I read your article from yesterday, so I know about Tuples. How do I actually use one? \u2014HD Hello HD, Microsoft Scripting Guy, Ed Wilson, is here. I just got back from working out with my trainer. I am not nearly as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=747"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}