Work with foreach and while

Hi people,

Here another challenge…for me, because for you that is very easy… :slight_smile:

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.

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++
}

I’d probably go more for…

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

$DataTable

I told! you are the best of the best!

Thank you so much. the script works fine.

thanks a lot.

rc.