Powershell function New-Array

February 7, 2008 at 5:02 PMAndy Schneider

Powershell can slice and dice arrays all day long, and we use them all the time. However, when we are testing stuff and creating dummy arrays the syntax can be a bit awkward. Its quite straightforward and understandable, but its just a pain to type

For example

   1: PS 39 >  $i = "one","two","three"
   2: PS 40 >  $i
   3: one
   4: two
   5: three

Now for me that is just way too many quotes and commas. This can easily be addressed with a new function called New-Array.  Are we going to have to parse and add quotes and do all kinds of crazy stuff to get this working ? Not really. Here's the function and how to use it.

   1: PS 41 >  function new-array {$args}
   2: PS 42 >  $i = new-array one two three four five six
   3: PS 43 >  $i
   4: one
   5: two
   6: three
   7: four
   8: five
   9: six

Ah, now that is much better. All this does is return the $args array which is an array that you get in every function be default. $args[0] is the first argument. $args[1] is the second etc etc.

That being said we are not quite there.  Lets say you want to be really sure you create an Array. In the case where you originally have only 1 $arg it will return a scalar. Let me demonstrate.

   1: PS 58 >  function new-array {$args}
   2: PS 59 >  $i = new-array 1
   3: PS 60 >  $i += 2
   4: PS 61 >  $i
   5: 3

 

In this case, $i ends up being a scalar with a value of 1 and then when you add 2 to it, you get $i = 3.

What we want is to be able to use the += operator to add a value to the array. There are multiple ways to force a scalar to be an array when you declare it but i think the comma operator is the shortest.  So we make one quick addition to our new-array function.

   1: PS 62 >  function new-array {,$args}
   2: PS 63 >  $i = new-array 1
   3: PS 64 >  $i +=2
   4: PS 65 >  $i
   5: 1
   6: 2

Pretty cool!

Posted in:

Tags:

Comments (2) -

I'm having a similar difficulty: I want a function to ALWAYS return an array, even if there is one element.  However, it's ALWAYS converting the array to a scalar when it returns it, even if I add a comma before the variable being returned.  I even tried adding your new-array function to the return, and it's still not working:

PS > function new-array {,$args}
PS > $i = new-array 1
PS > $i
1
PS > $i.count
1
PS > function a {$x=1; return new-array $x}
PS > $x = a
PS > $x
1
PS > $x.count
PS >

Am I doing something wrong here?  I really, REALLY don't want to change every place I call this function to force the array -- I want to force the array in the function, so that calling it uses the same syntax as calling any other function.

Any ideas?

Thanks,
Ken

Reply

how can I create a bidimensional array and sort it by 1 dimension
    0        1
[1.23][123]
[3.56][356]
[2.56][256]
[1.00][100]

I want to sort by column 1so I can return column 0 in the right order
[1.00] 1.00.cs
[1.23]  1.23.cs
[2.56] 2.56.sql
[3.56]  3.56.sqc

The number are actually filenames with extension but some how they come in the wrong order because of the dots

Reply

Pingbacks and trackbacks (1)+

Add comment