2016-11-09 42 views
0

試圖找到我的文件中的數字可以被3整除。我怎樣才能讓我的每個循環讀取每個數字單獨?Powershell - 我該如何製作

this is my file: 
    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" 

回答

3

你是如何走到這一步的,而沒有意識到沒有的,即使做了什麼呢?無論您使用哪種開發方式,都需要重新考慮。

提示什麼是錯的:

`import-csv "document 2.Dat"

它是如何印刷更多的破折號比甚至有在文件中?它實際上是在打印什麼?使用有用的調試/測試工具,包裹在破折號每一件事情,所以你可以看到,他們開始和結束:

<code>Import-Csv 'D:\document 2.dat' | foreach { "-$_-" }</code>

哦,那是壞了。

裏面function number {write-host 'hello?'看到它從來沒有打印任何東西。

嘗試手工調用函數來看看它做什麼:

<code>number '5 6 7'</code>

哦,我不知道數量不能被3整除,我最好的修復程序,所以我可以看到這是怎麼回事上。

如果你有一隻眼睛在尋找細節

<code>screenshot of function code</code>

哪裏$line得到分配?什麼是=if測試中做什麼? % 3什麼都不做在%的左邊?爲什麼我會使用變量名稱,如$a$b,這些變量名稱不能幫助我理解所發生的事情?

,當然,「*爲什麼我還不write-host "..."一路過關斬將,和/或通過此代碼在調試器步進,看看發生了什麼?


  1. 谷歌(*)」讀文件的powershell」

google for "read file powershell"

  • 嘗試
  • get-content 'd:\document 2.dat'

    這是我的文件好嗎。輸出的限制是......線。涼。

  • function number我應該給它一個更好的名字但是。
  • 嘆息。好吧好吧。

    function Get-NumbersWhichDivideEvenlyByThree

    無輸出,即使從簡單的 '喜'?啊,調用函數。

    Call function, see output

    大。

    參數傳遞給它,並打印出來......

    change function to accept a param and print it

    無輸出。

    足夠的屏幕截圖。

    1. 調用函數時傳遞參數。 Get-NumbersWhichDivideEvenlyByThree $FileContent

    2. 遍歷線及打印功能裏面。

    3. 谷歌「的PowerShell得到串號」和東西

    4. 迭代開發你的代碼,從工作塊工作塊去。永遠不要在你有一個十幾行的所有不半打不同的方式工作,一下子,也無處可從那裏的位置結束。

    位,你居然問

    獲取號碼的開出一個字符串。

    使用正則表達式。這正是他們存在的原因。但要儘量保持簡單 - 的方式,實際上是更復雜,但堅韌的 - 除了打破空間的線條,並挑選出哪些是數字的碎片,扔剩下的路程。

    要與合理不錯的答案得到這個,你幾乎需要神奇地瞭解-split,也許絆倒在這裏@ mklement0的答案約unary splitsplit has an unary formthe unary form of the -split operator is key here,或者,我猜想之一,在仔細看過help about_Split詳情。

    -split '6 9 7' # this splits the line into parts on *runs* of whitespace 
    
    6 
    9 
    7 # look like numbers, but are strings really 
    

    所以,你得到一些文字作品,包括在文件中-----行,那將是其中之一。而你需要測試它是數字,並保持他們的,哪些是破折號(字母,標點符號等),並拋出這些了。

    $thing -as [int] # will try to cast $thing as a (whole) number, and silently fail (no exception) if it cannot. 
    
    # Split the line into pieces. Try to convert each piece to a number. 
    # Filter out the ones which weren't numbers and failed to convert. 
    $pieces = -split $line 
    $pieces = $pieces | ForEach-Object { $_ -as [int] } 
    $numbers = $pieces | Where-Object { $_ -ne $null } 
    

    然後你可以做% 3測試。而有這樣的代碼:

    function Get-NumbersWhichDivideEvenlyByThree { 
        param($lines) 
    
        foreach ($line in $lines) 
        { 
    
         $pieces = -split $line 
         $pieces = $pieces | ForEach-Object { $_ -as [int] } 
         $numbers = $pieces | Where-Object { $_ -ne $null } 
    
         foreach ($number in $numbers) 
         { 
          if (0 -eq $number % 3) 
          { 
           Write-Output "$number divisible by 3" 
          } 
          else 
          { 
           Write-Output "$number not divisible by 3" 
          } 
         } 
    
        } 
    } 
    
    $FileContent = Get-Content 'D:\document 2.dat' 
    Get-NumbersWhichDivideEvenlyByThree $FileContent 
    

    和輸出,如:

    Example output of working program


    1. (-split(gc D:\test.txt -Raw)-match'\d+')|%{"$_$(('',' not')[$_%3]) divisible by 3"}
    +1

    非常感謝你的幫助!我應該更好地組織我的問題,我很抱歉。 $ line在我的腳本中是$ a,我必須在錯誤的時刻複製它。我可以部分地使我的腳本工作,因爲我先前加載了另一個文件。再次感謝您爲我解決這個問題! – SkullNerd