_______________________________________________________________________________________

Note: This e-mail is subject to the disclaimer contained at the bottom of this 
message.
_______________________________________________________________________________________


Hmmm isn't that when you use the 'For' not 'foreach'

$arrAnimals = @("dog", "cat", "horse", "snake")

for (0; $idx -le $arrAnimals.length ;$idx++)
{
    Write-Host ("Loop count: " + $idx)
    Write-Host $arrAnimals[$idx]
}



From: [email protected] [mailto:[email protected]] On 
Behalf Of Aakash Shah
Sent: Thursday, 15 August 2013 1:19 PM
To: [email protected]
Subject: RE: [powershell] Current Position In ForEach Loop

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]> 
[mailto:[email protected]] On Behalf Of CESAR.ABREG0 .
Sent: Wednesday, August 14, 2013 7:35 PM
To: [email protected]<mailto:[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]<mailto:[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

_______________________________________________________________________________________

The information transmitted in this message and its attachments (if any) is 
intended 
only for the person or entity to which it is addressed.
The message may contain confidential and/or privileged material. Any review, 
retransmission, dissemination or other use of, or taking of any action in 
reliance 
upon this information, by persons or entities other than the intended recipient 
is 
prohibited.

If you have received this in error, please contact the sender and delete this 
e-mail 
and associated material from any computer.

The intended recipient of this e-mail may only use, reproduce, disclose or 
distribute 
the information contained in this e-mail and any attached files, with the 
permission 
of the sender.

This message has been scanned for viruses.
_______________________________________________________________________________________


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to