2011-03-05 39 views
2

我有三个我想打印的数字列表。这三个列表是三个数字集合。他们有相同的号码,以便从所有三个的第一位置开始的元素,到最后一个,我必须打印这样的事情在表中格式化使用PowerShell的三个数据列表

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 

... 

如何做到这一点使用格式表? 或更好的,我怎样才能使用Format-Table cmd-let来解决这个问题?

三江源

回答

3

这里有一个解决方案:

$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; 

结果于:

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


说明

的@ {N = “嗒嗒” 的每个实例; e = { blah}}是散列表。这些键是“名称”和“表达式”的简称。见示例4 on this page

“$ _”表示正在输入的值。在这种情况下,每个索引值都被输入到select语句中。

+0

请问您能解释我做了什么吗?那么我认识到像创建一个关联数组......关于@ oerator的帽子?谢谢 – Andry 2011-03-06 10:59:09

1

我是一个PowerShell的新手,我敢肯定,有更好的方式,但我希望这个例子可以帮助你

$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 
2
$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