Thanks Daniel! Yes, that will work for the condition case below, but if there
happens to be duplicate entries in the array, this will not work. So extending
your example below for instance, the following will not work correctly:
$array = "dog","cat","bird","dog"
Foreach ($animal in $array) {
$animal
[array]::IndexOf($array,$animal)
}
Since the results will produce:
dog
0
cat
1
bird
2
dog
0
Thanks,
-Aakash Shah
From: [email protected] [mailto:[email protected]] On
Behalf Of Daniel Ratliff
Sent: Thursday, August 15, 2013 7:23 PM
To: '[email protected]'; '[email protected]'
Subject: RE: [powershell] Current Position In ForEach Loop
Without the typo. $animals should be $animal.
-Daniel Ratliff
-----Original Message-----
From: Daniel Ratliff [[email protected]<mailto:[email protected]>]
Sent: Thursday, August 15, 2013 10:20 PM Eastern Standard Time
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] Current Position In ForEach Loop
Took some playing around but I think this will work for you.
$array = “dog”,”cat”,”bird”
Foreach ($animal in $array) {
$animal
[array]::IndexOf($array,$animals)
}
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Aakash Shah
Sent: Thursday, August 15, 2013 9:57 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] Current Position In ForEach Loop
Yes, that could work too. But that still requires that I initialize $idx and
increment it. What I was trying to find out is if there was a built in
mechanism where I could call something like “$ForEach.CurrentPosition” to help
identify what iteration loop I was on without needing to initialize and
increment an index variable.
Thanks,
-Aakash Shah
From:[email protected]<mailto:[email protected]>
[mailto:[email protected]]<mailto:[mailto:[email protected]]>
On Behalf Of Magnus Tveten
Sent: Thursday, August 15, 2013 5:41 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] Current Position In ForEach Loop
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]>
[mailto:[email protected]] On Behalf Of Aakash Shah
Sent: Thursday, 15 August 2013 1:19 PM
To: [email protected]<mailto:[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
================================================
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 is intended only for the person or entity to which
it is addressed
and may contain CONFIDENTIAL material. If you receive this material/information
in error,
please contact the sender and delete or destroy the material/information.
================================================
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 is intended only for the person or entity to which
it is addressed
and may contain CONFIDENTIAL material. If you receive this material/information
in error,
please contact the sender and delete or destroy the material/information.
================================================
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