{"id":751,"date":"2014-09-01T00:01:00","date_gmt":"2014-09-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/01\/whats-a-tuple-and-why-would-i-want-to-use-it\/"},"modified":"2014-09-01T00:01:00","modified_gmt":"2014-09-01T00:01:00","slug":"whats-a-tuple-and-why-would-i-want-to-use-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/whats-a-tuple-and-why-would-i-want-to-use-it\/","title":{"rendered":"What&#8217;s a Tuple, and Why Would I Want to Use It?"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Learn about using tuples with Windows PowerShell.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! What is a tuple, and why would I want to use one?<\/p>\n<p>&mdash;RT<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello RT,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I just got back from the gymnasium, and I am sitting on the back porch sipping a glass of water, trying to cool down. I decided last week to begin an exercise regime. I know exercise is supposed to make one feel better and all that, but so far, I feel worse&mdash;much worse. I am so sore that even my eyelids hurt. Oh well, I am sure it gets easier. Hmmm, I wonder if my trainer will take it easy on me if I bring donuts tomorrow&hellip;<\/p>\n<h2>What&rsquo;s a tuple?<\/h2>\n<p>A tuple is a data structure that has a specific number and sequence of elements. In .NET Framework&nbsp;4.5, I can create a tuple with up to seven elements. If I need eight or more elements, I nest tuple objects in the <b>Rest<\/b> property of the tuple.<\/p>\n<p>An example of a tuple, might be a tuple with three elements. This would be known as either a 3-Tuple, or a triple. In this three-element tuple, I might decide I want to store a name, an ID number, and a phone number.<\/p>\n<h2>What is a tuple good for?<\/h2>\n<p>There are four main ways that tuples are used:<\/p>\n<ul>\n<li>To represent a single set of data. This is like storing the results of a single record from a database. The tuple itself would be like the database record, and the elements of the tuple would be the fields contained in the record.<\/li>\n<li>To provide easy access to data and provide easy manipulation of that data.<\/li>\n<li>To return multiple values from a method without having to use <b>Out<\/b><i> <\/i>parameters. In Windows PowerShell, this would be an easy way to return multiple values from a function without having to create an intermediate object.<\/li>\n<li>To pass multiple values to a method through a single parameter. In Windows PowerShell, this is a way to pass multiple values to a single parameter of a function.<\/li>\n<\/ul>\n<p>In.NET Framework 4.5, there is a new System.Tuple class that provides static methods for creating a tuple object. This makes it easier to create and use a tuple in Windows PowerShell, as opposed to having to use the <b>New-Object<\/b> cmdlet and calling a specific constructor to create a new tuple.<\/p>\n<p>Basically the System.Tuple class provides a single static method called <b>Create<\/b><i> <\/i>that permits one to create a new tuple object. But it can be used to create a tuple with from one to eight elements in it. This goes from a singleton (a new 1-tuple) to an &ldquo;octuplet&rdquo; (or an 8-tuple). It is possible to create a tuple with more than 8 elements, but that requires using the <b>Rest<\/b><i> <\/i>property to nest tuple objects. I will talk about that property later.<\/p>\n<p>For more information about tuples and the various members of the <b>Tuple<\/b> class, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.tuple(v=vs.110).aspx\" target=\"_blank\">Tuple Class<\/a> on MSDN.<\/p>\n<h2>Creating a singleton<\/h2>\n<p>In reality, a singleton (a 1-tuple), is rather rare because it is not all that useful. But it is useful from a documentation or from a simple example standpoint.<\/p>\n<h3>Use the helper methods<\/h3>\n<p>One of the cool things about.NET Framework&nbsp;4.5 is the helper methods for creating tuples. I do not need to use <b>New-Object<\/b>&mdash;I can simply call the appropriate <b>Create<\/b> method and create the appropriate tuple. Here is an example of how to create a singleton (or a 1-tuple) and access the data stored in the tuple.<\/p>\n<p style=\"margin-left:30px\">$T = [system.tuple]::Create(&quot;Flintstone&quot;)<\/p>\n<p style=\"margin-left:30px\">&quot;Fred&#039;s last name is {0}&quot; -f $t.item1<\/p>\n<p>To access the element, I use the <b>Tuple<\/b> object stored in the <b>$t<\/b> variable, and I access the element by item number. In this case, the first element is <b>item1<\/b><i>, <\/i>and that is what I use. I use the format operator, and I substitute for <b>{0}<\/b> in my string. When I run the script, the following output appears:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-1-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-1-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to creating a tuple. Join me tomorrow when I will talk about using a tuple in a Windows PowerShell script.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn about using tuples with Windows PowerShell. &nbsp;Hey, Scripting Guy! What is a tuple, and why would I want to use one? &mdash;RT &nbsp;Hello RT, Microsoft Scripting Guy, Ed Wilson, is here. I just got back from the gymnasium, and I am sitting on the back porch sipping a glass of water, trying to [&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-751","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 about using tuples with Windows PowerShell. &nbsp;Hey, Scripting Guy! What is a tuple, and why would I want to use one? &mdash;RT &nbsp;Hello RT, Microsoft Scripting Guy, Ed Wilson, is here. I just got back from the gymnasium, and I am sitting on the back porch sipping a glass of water, trying to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/751","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=751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/751\/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=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}