2016-11-09 87 views
0

試圖找到我的文件中的數字可以被3整除。我怎樣才能讓我的foreach循環分別讀取每個數字?我試圖弄清楚如何將文件放入數組中,能幫助我嗎?Powershell - 如何將我的.dat文件放入數組中?

這是我的文件:

6 9 7 
----- 
5 2 9 
3 4 4 
1 6 9 

這是到目前爲止我的代碼:

function number{ 
    param($b) 

    # Loop through all lines of input 
    foreach($a in $b){ 

     if ($line = % 3) 
      { 
      Write-Output "divisible by 3" 
     } 
     else { 
      Write-Output "number not divisible by 3" 
     } 
     } 
     } 


#Variables 

#Get input from csv file 
$a = Import-Csv "document 2.Dat" 

回答

1

試試這個

$myarray=get-content C:\temp\test.csv | %{$_ -split " "} | %{if ($_ -match "\w+" -and ([int]$_ % 3) -eq 0) {[int]$_}} 
相關問題