Monday, May 7, 2012

Table of an array

I had a a PSObject with to fields [bool]"Done" and [string]"Name".  To signify the status of some production items.
I need these results to be human readable and not print pages of data.
I don't propose that this is the best solution, but this is what I came up with as a quick solution.

This forces each name to a 20 character column and puts for columns on the page at once.


$data2 | Group {$_.Done} | %{
            $_.Name
            $ColumnCount = 4
            $Count = $_.Group.Count
            For  ($index = 0$index -le $Count;$index+=$ColumnCount)
            {            
              For ($i=0;$i -lt $columnCount;$i++)
              {
                if ($index+$i -lt $Count) {
                    Write-host $_.Group[$index+$i].Name.Padleft(20-NoNewline              
                }
              }
              Write-host            
            }
        }

Try number 2 breaks out the old modulus function.  I asked Will if he had an answer and did a quick "Is this what you mean?" using a 1..11 array.  Seeing the array as numbers modulus was the clear grouping method.


$data2 | Group {$_.Done} | %{
            Write-host $_.Name
            $ColumnCount = 6           
            For  ($index = 0$index -lt $_.Group.Count;$index++)
            {
              Write-host $_.Group[$index].Name.Padleft(20-NoNewline               
              if ($index $ColumnCount -eq 0) { Write-host }
            }
            Write-host
        }


I also considered, and I may yet revise my solution, creating a PSObject with 4 members and assign them in a similar fashion.  This would allow me to ConvertTo-CSV, and use the normal Format-Table.


No comments:

Post a Comment