Work with foreach and while

This topic contains 3 replies, has 3 voices, and was last updated by  Rodrigo 1 week, 4 days ago.

  • Author
    Posts
  • #98697

    Rodrigo
    Participant

    Hi people,

    Here another challenge...for me, because for you that is very easy... 🙂

    I need get some information trough snmp. But, the information is an table. I need to get only the data greater than zero. The presentation will be mor or less like below:

    get data:

    456
    789
    4355
    0
    7899
    0
    23456
    0
    0
    0

    Now, I need remove all zeros:

    456
    789
    4355
    7899
    23456

    And then represent the data with an id:

    "data01 456"
    "data02 = 789"
    "data03 = 4355"
    "data04 = 7899"
    "data05 = 23456"

    Remember, the "data0x" are created based of array...

    Did I was clear?

    Thank you so much.

    rc.

  • #98701

    L-Bo
    Participant

    This should work...

    $data = @(
    456
    789
    4355
    0
    7899
    0
    23456
    0
    0
    )
    
    $x = 0
    Foreach ($d in ($data -gt 0))
    {
      "Data$("{0:d3}" -f  $x):$d"
      $x++
    }
    • #98710

      Rodrigo
      Participant

      I told! you are the best of the best!

      Thank you so much. the script works fine.

      thanks a lot.

      rc.

  • #98704

    Joel Sallow
    Participant

    I'd probably go more for...

    $DataTable = @{}
    $Index = 0
    $Array | Where-Object {$_} |
        ForEach-Object {
            $Index++
            $ID = "Data{0:D2}" -f $Index
            $DataTable[$ID] = $_
        }
    
    $DataTable

You must be logged in to reply to this topic.