Array, or not array…

This time, I have a bit less shiny topic to talk about, but it is something you probably will run into and may cost you some hours of sleep. It’s about the PowerShell behavior in returning values from functions. There are 2 specific situations I’d like to point out.One is when you expect to return a single value and instead you get an array, and the other is when you expect to get an array and instead you get a single value. As PowerShell is the underlying language for tuning Data Standard, I’m sure you will benefit from this post.

Let’s start with the first situation. Your function should return a single value, but instead it returns an array, and you don’t understand where the additional values are coming from. Let me make simple example:

function testSingleValue
{
  "1"
  "2"
  "3"
  return "4"
}

testSingleValue

This function returns the number 4 as a string, right? Wrong! It returns an array with the values “1”,”2”,”3”,”4”. The reason is that the strings “1”,”2” and “3” are not saved into a variable, so the output of those strings goes into the PowerShell pipeline, which collects the outputs of each line and returns all the values as an array. Ok, this function does not make any sense, but suppose you have a command in your function that returns a value, and that value is not captured into a variable. Here a sample:

function HelloWorld
{
  [System.Windows.Forms.MessageBox]::Show("Hello World!")
  return "Hello World"
}

HelloWorld

The function simply shows a MessageBox with “Hello World” and returns a fixed string “Hello World”. However, when the function is called, the returned value of our function will be an array containing “OK” and “Hello World”. The Show command returns the selected button ( [OK] ), and that value is added to the pipeline. In order to avoid such behavior, simply store the return value of the Show function into a variable. If you don’t want to waste a variable, then use the $null variable, like this:

function HelloWorld
{
  $null = [System.Windows.Forms.MessageBox]::Show("Hello World!")
  return "Hello World"
}

HelloWorld

At this point, you may think “what is then the return command doing, if it is not returning the given parameter?”. Well, it ends the function and passes the argument to the pipeline, too. In reality, it is not necessary to use the return command at all. This would work as well:

function HelloWorld
{
  $null = [System.Windows.Forms.MessageBox]::Show("Hello World!")
  "Hello World"
}

HelloWorld

The function would return the string “Hello World”, because it is sent to the pipeline. Bottom line: pay attention to all functions that return a value, or in case you know how this works, feel free to send messages to the pipeline. They will be collected and returned as an array.

The second situation is the opposite. You expect that an array should be returned, but instead a single value is returned. So, let’s try this:

function testArray
{
  $myArray = @("1","2","3")
  return $myArray
}
$myVar = testArray

This function returns an array with 3 elements, as expected. Let’s try the same with just one element:

function testArray
{
  $myArray = @("1")
  return $myArray
}

$myVar = testArray

Now, you would expect that the variable $myVar is an array with just one element. Well, as the array contains just one element, PowerShell transforms the array into the single element, in this case the string “1”. In order to prevent this behavior, just add a comma (,) in front of the variable you like to return, like this:

function testArray
{
  $myArray = @("1")
  return ,$myArray
}

$myVar = testArray

You will notice that the variable $myVar will be an array with a single element, as expected. In the post Pick-List from custom objects, you will notice the same technique in the cOgetContacts function, as this function may return no elements, a list of elements, or just one element. In case of one element, PowerShell would return the single element, instead of an array with one element. You will also find this technique in the CAD-BOM tab delivered with Data Standard.

So, whether you have too many elements in your return value or too few, the first or the second scenario might be the problem/solution to your headache. I wish you restful nights with Data Standard and PowerShell ;-)

This entry was posted in Data Standard, PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s