2016-11-09 35 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

    相关问题