ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Grrr!
Sudoku »
 

Minor gripes

04 Jan 2007 12:09 pm// Rant    

A number of little things bother me about PowerShell, and I’d just like to outline them here.

Adding to arrays

According to get-help about_array :

“You can append an element to an existing array by using the += operator. When the operator is used on the array itself, it appends the value.”

This isn’t completely true. If the right hand side of the += is also an array, then the elements of that array are added, not the array itself. This makes it very difficult to build up an array of arrays:

$a = @() # start with an empty array
$a += 1,2 # this doesn't work - it adds 1 and 2

so you need to do:

$a += 1
$a[-1] = 1,2

I think that either a) the documentation be updated to make this clear or b) fix += so it really does add the RHS as a new element. Otherwise, I’m going to have to use something like this, which is a bit ugly (but actually much more efficient):

$a = new-object collections.ArrayList
$a.add( @(1,2) )

Update: I’ve since discovered you can do this instead with the unary comma operator followed by a parenthesised list. The syntax is a bit weird (and hard to guess) but I believe the next version of PowerShell will get better documentation in this area.

$a=@()
$a += ,(1,2)

Integer division

Many languages support integer division, yielding an integer result. Many languages compliment this by offering a modulus division aswell. This is great, because one often has an index into a pseudo-2d array and you want to get the row and column values. Notionally:

width=10
offset = 17
row = offset DIV width
column = offset MOD width

This should yield row=1, column=7

In PowerShell:

[int]$width = 10
[int]$offset = 17
[int]$row = $offset / $width
[int]$column = $offset % $width

This yields a row of 2. The division, even though both sides are [int] is done using floating point and rounded up.

To work around it, we have to do this:

[int]$row = [Math]::Floor($offset/$width)

Not sure of a sensible fix for this… maybe a new operator like -idiv ? or the pythonesque //

Comments are closed.

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.