# Work with foreach and while

**URL:** <https://forums.powershell.org/t/work-with-foreach-and-while/10508>\
**Category:** PowerShell Help\
**Created:** [April 13, 2018, 12:39pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508 "2018-04-13T12:39:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 13, 2018, 12:39pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508/1 "2018-04-13T12:39:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bo1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bo1/32/33_2.png) [@bo1](https://forums.powershell.org/u/bo1)\
**Post date:** [April 13, 2018, 1:59pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508/2 "2018-04-13T13:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [April 13, 2018, 6:48pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508/3 "2018-04-13T18:48:19Z")

</div>

I’d probably go more for…

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

$DataTable
```

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [April 13, 2018, 8:13pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508/4 "2018-04-13T20:13:43Z")

</div>

I told! you are the best of the best!

Thank you so much. the script works fine.

thanks a lot.

rc.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:35pm UTC](https://forums.powershell.org/t/work-with-foreach-and-while/10508/5 "2024-05-16T20:35:57Z")

</div>


