My understanding is it is a limitation of the underlying. Net framework and how it handles enumerating collections. On Aug 15, 2013 12:04 AM, "Aakash Shah" <[email protected]> wrote:
> Thanks for the confirmation. > > -Aakash Shah > > -----Original Message----- > From: [email protected] [mailto: > [email protected]] On Behalf Of Mark Stang > Sent: Wednesday, August 14, 2013 8:48 PM > To: [email protected] > Subject: Re: [powershell] Current Position In ForEach Loop > > No there is no built in loop counter. You have to add your own if you > need one. > > On Wed, Aug 14, 2013 at 11:19 PM, Aakash Shah <[email protected]> wrote: > > Thanks for responding. > > > > > > > > I am able to access the array value at a specific index. So in the > example > > below, I am able to successfully access the letter "c" if I use > > "$arrLetters[2]". > > > > > > > > However, what I would like to do is be able to identify what iteration > loop > > number the script is currently processing from within the foreach loop > using > > a built in method without needing to manually create and increment a > counter > > variable. > > > > > > > > One example of where I would like to use this is: > > > > > > > > $arrAnimals = @("dog", "cat", "horse", "snake") > > > > > > > > foreach ($strAnimal in $arrAnimals) > > > > { > > > > Write-Host ("Loop count: " + <ForEachLoopCounter>) > > > > Write-Host $strAnimal > > > > } > > > > > > > > Is there a built in method to determine what the <ForEachLoopCounter> is > > without needing to create a counter variable and increment it manually on > > each iteration? > > > > > > > > Thanks, > > > > > > > > -Aakash Shah > > > > > > > > From: [email protected] [mailto: > [email protected]] > > On Behalf Of CESAR.ABREG0 . > > Sent: Wednesday, August 14, 2013 7:35 PM > > To: [email protected] > > Subject: Re: [powershell] Current Position In ForEach Loop > > > > > > > > I'm most likely wrong but wouldnt this give you C on index 0. > > $strLetter [2] > > > > @ the beach now ;) but will check it when I get home. PowerGUI gives you > > this info easy when debuging > > > > On Aug 14, 2013 7:07 PM, "Aakash Shah" <[email protected]> wrote: > > > > Is there an elegant way to get the current position in a ForEach loop > > without needing to initiate a variable and then increment it on each > > iteration? > > > > > > > > For example, if I have the code below: > > > > > > > > $arrLetters = @("a", "b", "c", "d") > > > > > > > > foreach ($strLetter in $arrLetters) > > > > { > > > > Write-Host $strLetter > > > > } > > > > > > > > In this example, if the loop reaches "c", is there some built in method > to > > discover that the current iteration loop is # 2 (or #3 if the index > starts > > at 1 instead of 0). > > > > > > > > It's not necessarily a problem to initiate a "counter" variable, but I > was > > just curious to know if there is a cleaner way. > > > > > > > > I am using PowerShell 3 in case that helps. > > > > > > > > Thanks! > > > > > > > > -Aakash Shah > > > > > > > > > > ================================================ > > Did you know you can also post and find answers on PowerShell in the > forums? > > http://www.myitforum.com/forums/default.asp?catApp=1 > > > > > > ================================================ > > Did you know you can also post and find answers on PowerShell in the > forums? > > http://www.myitforum.com/forums/default.asp?catApp=1 > > > > > > ================================================ > > Did you know you can also post and find answers on PowerShell in the > forums? > > http://www.myitforum.com/forums/default.asp?catApp=1 > > > ================================================ > Did you know you can also post and find answers on PowerShell in the > forums? > http://www.myitforum.com/forums/default.asp?catApp=1 > > > > > > ================================================ > Did you know you can also post and find answers on PowerShell in the > forums? > http://www.myitforum.com/forums/default.asp?catApp=1 > > ================================================ Did you know you can also post and find answers on PowerShell in the forums? http://www.myitforum.com/forums/default.asp?catApp=1
