September 1st, 2014

What’s a Tuple, and Why Would I Want to Use It?

Doctor Scripto
Scripter

Summary: Learn about using tuples with Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! What is a tuple, and why would I want to use one?

—RT

Hey, Scripting Guy! Answer 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 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—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…

What’s a tuple?

A tuple is a data structure that has a specific number and sequence of elements. In .NET Framework 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 Rest property of the tuple.

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.

What is a tuple good for?

There are four main ways that tuples are used:

  • 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.
  • To provide easy access to data and provide easy manipulation of that data.
  • To return multiple values from a method without having to use Out parameters. In Windows PowerShell, this would be an easy way to return multiple values from a function without having to create an intermediate object.
  • 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.

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 New-Object cmdlet and calling a specific constructor to create a new tuple.

Basically the System.Tuple class provides a single static method called Create 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 “octuplet” (or an 8-tuple). It is possible to create a tuple with more than 8 elements, but that requires using the Rest property to nest tuple objects. I will talk about that property later.

For more information about tuples and the various members of the Tuple class, see Tuple Class on MSDN.

Creating a singleton

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.

Use the helper methods

One of the cool things about.NET Framework 4.5 is the helper methods for creating tuples. I do not need to use New-Object—I can simply call the appropriate Create 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.

$T = [system.tuple]::Create("Flintstone")

"Fred's last name is {0}" -f $t.item1

To access the element, I use the Tuple object stored in the $t variable, and I access the element by item number. In this case, the first element is item1, and that is what I use. I use the format operator, and I substitute for {0} in my string. When I run the script, the following output appears:

Image of command output

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.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.