$foreach - Properties of enumerators?

Welcome Forums General PowerShell Q&A $foreach - Properties of enumerators?

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 11 months ago.

  • Author
    Posts
  • #32255

    Participant
    Points: 1
    Rank: Member

    I'm using a foreach loop to iterate over an array. I've been using $array.indexof($element) to get the index of the current item being processed to use in inspecting a parallel array. I've seen mention of a $foreach variable or object that might be more effective and simpler than IndexOf but I can't find any documentation on it. Unfortunately Google and Bing search don't see a difference between "$foreach" and "foreach" so that isn't helping.

    Edit:

    So I found some docs on $foreach but they state that it is an enumerator and "you can use all the properties and methods of enumerators". Started looking for docs on the properties and methods of enumerators and no luck there either.

    TIA

  • #32268

    Participant
    Points: 0
    Rank: Member

    Here is an example:

    In a typical ForEach loop, you would do something like this:

    ForEach ($a in 1..10) {
        $a
    }
    

    Which would result in:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    In this case, ForEach is an enumerator that enumerates the numbers 1 through 10 into the variable $a one at a time. $foreach is a variable reference back to the enumerator so you can access properties or method of the foreach loop itself. For example, say you wanted to skip every other value in the enumeration. You can use the MoveNext method of the enumerator inside of the loop.

    ForEach ($a in 1..10) {
        $a
        $ForEach.MoveNext()
    }
    

    Results in:

    1
    True
    3
    True
    5
    True
    7
    True
    9
    True
    

    Obviously you can use | out-null if you don't want to see the True/False result

    The available properties and methods for an enumerator can be found here:
    https://msdn.microsoft.com/en-us/library/system.collections.ienumerator(v=vs.110).aspx

  • #32270

    Participant
    Points: 1
    Rank: Member

    Thanks for the link. I was hoping the $foreach enumerator would give me access to the current index in the foreach loop. I don't see a property on $foreach (enumerators in general) that provides the index, just the value of the current item.

    Is there a way to get the current index in a foreach loop? Or do I need to keep my own pointer or change it to a for loop (i.e. for (I=0;i<$a.length;i++))?

  • #32273

    Participant
    Points: 0
    Rank: Member

    I think for a parallel array comparison, for($i=0;$i -lt $a.length;$i++) is probably the simplest approach.

    $a = 1..10
    $b = 10..1
    for ($i=0;$i -lt $a.length;$i++) {
        "$($a[$i]) - $($b[$i])"
    }
    

    Results:
    1 – 10
    2 – 9
    3 – 8
    4 – 7
    5 – 6
    6 – 5
    7 – 4
    8 – 3
    9 – 2
    10 – 1

  • #32274

    Participant
    Points: 1
    Rank: Member

    That's where I settled but I'm a very newbie so I figured I would ask to see if there was a better way.

    Thanks.

The topic ‘$foreach - Properties of enumerators?’ is closed to new replies.