Powershell - 我该怎么做

时间:2016-11-09 02:58:17

标签: powershell

试图找到我的文件中可以被3整除的数字。如何让每个循环单独读取每个数字?

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"

1 个答案:

答案 0 :(得分: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. Google(*)“读取文件powershell”
  2. google for "read file powershell"

    1. 试一试
    2. get-content 'd:\document 2.dat'

      这是我的档案好。输出的限制是......线。凉。

      1. function number我应该给它一个更好的名字,但是。
      2. 。好吧,好吧。

        function Get-NumbersWhichDivideEvenlyByThree

        没有输出,即使是简单的'hi'?啊,调用函数。

        Call function, see output

        将参数传递给它并打印出来......

        change function to accept a param and print it

        没有输出。

        足够的截图。

        1. 调用函数时传递参数。 Get-NumbersWhichDivideEvenlyByThree $FileContent

        2. 迭代线条并在函数内打印。

        3. Google“powershell从字符串中获取数字”和内容

        4. 迭代开发代码,从工作块到工作块。永远不会在你有十几条线路的位置上,这些线路一次都不能以六种不同的方式工作,而且无处可去。

        5. 您实际要求的位

          从字符串中获取数字。

          使用正则表达式。这正是他们存在的原因。但是试着保持简单 - 以一种实际上更复杂但又更难的方式 - 在空间上分开线条,挑出数字并将其余部分扔掉。

          为了得到一个相当不错的答案,你几乎需要神奇地了解-split,或许是通过绊倒@ mklement0的答案来解决unary splitsplit has an unary form或{ {3}},或者,我想,仔细阅读了the unary form of the -split operator is key here

          -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
          

          输出如:

          help about_Split

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