Unary comma.

Have you ever had a problem that you wanted to do something with a collection as a whole and you couldn’t? I had this issue several times. Most obvious and most common problem is discovering collection properties. Is there a length property on the collection? Let’s see:

PowerShell, using GeSHi 1.0.8.8
  1. (1,2,3,4) | gm -MemberType Property

It returns literally nothing. Why? Because PowerShell assumes you want to get member of elements rather than whole collection. So what should you do? Use unary comma:

PowerShell, using GeSHi 1.0.8.8
  1. ,(1,2,3,4) | gm -MemberType Property
  2. TypeName: System.Object[]
  3. Name           MemberType Definition
  4. —- ———- ———-
  5. IsFixedSize    Property   System.Boolean IsFixedSize {get;}
  6. IsReadOnly     Property   System.Boolean IsReadOnly {get;}
  7. IsSynchronized Property   System.Boolean IsSynchronized {get;}
  8. Length         Property   System.Int32 Length {get;}
  9. LongLength     Property   System.Int64 LongLength {get;}
  10. Rank           Property   System.Int32 Rank {get;}
  11. SyncRoot       Property   System.Object SyncRoot {get;}

I had problem with that when I was completing my Inventory module. In this module I create two collection types: one is build on top of System.Collections.Hashtable and second is based on System.Collections.ArrayList. I’ve created functions within module to return both types. It was quite easy with hashtable:

PowerShell, using GeSHi 1.0.8.8
  1. Import-Csv $Path | ForEach-Object -Begin {
  2. $InventoryHash = New-Object PXL.Hash
  3. Write-Host -ForegroundColor Yellow “Processing file: $Path”
  4. } -Process {
  5. $InventoryHash.Add($_.Name,$(New-Object PXL.PC -Property @{
  6. Name=$_.Name
  7. SN=$_.SN
  8. User=$_.User
  9. Last=$_.Last
  10. Model=$_.Model
  11. SP=$_.SP
  12. Batt=$_.Batt
  13. MAC = $_.MAC
  14. tel = $_.tel
  15. InvDate=$_.InvDate
  16. PhysicalMemory=$([double]($_.RAM -replace ‘ GB’) * 1GB)
  17. FreeSpace=$([double]($_.Free -replace ‘ GB’) * 1GB)
  18. HDsize=$([double]($_.HD -replace ‘ GB’) * 1GB)
  19. })
  20. ) | Out-Null
  21. } -end {
  22. Write-Host -ForegroundColor Cyan ‘Done!’
  23. $InventoryHash
  24. }

With PXL.List I had to use unary comma, otherwise it would return System.Object[] – and that would not contain any of ScriptMethods I had prepared for my type.

PowerShell, using GeSHi 1.0.8.8
  1. # return object without unary comma….
  2. -end {
  3. Write-Host -ForegroundColor Cyan ‘Done!’
  4. $InventoryList
  5. }
  6. $inv = ImportInventoryList
  7. Processing file: A:\Inventory\Full.csv
  8. Done!
  9. $inv.GetType().FullName
  10. System.Object[]
  11. # same object, this time using unary comma
  12. -end {
  13. Write-Host -ForegroundColor Cyan ‘Done!’
  14. ,$InventoryList
  15. }
  16. $inv = ImportInventoryList
  17. Processing file: A:\Inventory\Full.csv
  18. Done!
  19. $inv.GetType().FullName
  20. PXL.List

Object preserved, methods there were I need them – all happy. 🙂 All thanks to unary comma. So if you are fighting with collection and banging your head against the desk – what I did wrong – maybe unary comma is all you need to move on. 🙂

Advertisements

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