Andry
Andry

Reputation: 16895

Formatting in a table three lists of data using PowerShell

I have three lists of numbers I would like to print. These three lists are three collections of number. They have the same number of elements so starting from the first position of all three, to the last one, I have to print something like this

Element in list1 pos1  |  Element in list2 pos1  |  Element in list3 pos1
Element in list1 pos2  |  Element in list2 pos2  |  Element in list3 pos2
Element in list1 pos3  |  Element in list2 pso3  |  Element in list3 pos3
Element in list1 pos4  |  Element in list2 pos4  |  Element in list3 pos4

...

How to do this using Format-Table? Or better, how can I use the Format-Table cmd-let in order to solve this?

Thankyou

Upvotes: 2

Views: 256

Answers (3)

Andrew Shepherd
Andrew Shepherd

Reputation: 45272

Here's one solution:

$c0=357,380,45
$c1=12,20,410
$c2=223,270,30

0..($c0.Length -1) | 
       select-object (
           @{n="one";   e={$c0[$_]}},
           @{n="two";   e={$c1[$_]}},
           @{n="three"; e={$c2[$_]}}
              ) | 
       format-table -auto;

Results in:

one two three
--- --- -----
357  12   223
380  20   270
 45 410    30


Explanation

Each instance of @{n="blah"; e={blah}} is a hashtable. The keys are short for "Name" and "Expression". See example 4 on this page.

"$_" represents the value that is being piped in. In this case, each index value is piped in to the select statement.

Upvotes: 3

mjolinor
mjolinor

Reputation: 68341

 $c0 = 357,380,45
 $c1 = 12,20,410
 $c2 = 223,270,30

 $x = foreach ($i in 0..(($c0.count)-1)){
 ($c0[$i],$c1[$i],$c2[$i]) -join "," | convertfrom-csv -header "c0","c1","c2" 
 } 

 $x | ft -auto

 c0  c1  c2 
 --  --  -- 
 357 12  223
 380 20  270
 45  410 30 

Upvotes: 2

Nicola Cossu
Nicola Cossu

Reputation: 56407

I'm a powershell newbie and I'm sure that there are better ways but I hope this example can help you

$e = @()
$a = 1,2,3,4
$b = 5,6,7,8
$c = 'nine','ten','eleven','twelve'
for ($i=0;$i -lt $a.count;$i++) {
$d = New-Object PSObject -Property @{            
        one       = $a[$i]
        two       = $b[$i]
        three     = $c[$i]
  }      
$e+=$d}
$e | select one,two,three | ft -auto

Upvotes: 1

Related Questions