![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Array Indexes The following returns all array items that equels to 3 PS C:\Scripts> @(1,2,3,5,3,2) -eq 3 3 3 How can I get the index numbers instead of the item content (i.e., 2,4)? Shay http://scriptolog.blogspot.com |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Array Indexes Shay Levi wrote: > > The following returns all array items that equels to 3 > > PS C:\Scripts> @(1,2,3,5,3,2) -eq 3 > 3 > 3 > > How can I get the index numbers instead of the item content (i.e., 2,4)? > > > Shay > http://scriptolog.blogspot.com > > arrays don't seem to have a lot of members to work with... You just need a simple solution? $i=0;@(1,2,3,5,3,2)|foreach-object{if($_ -eq 3){$i};$i++} Marco -- ---------------- PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Array Indexes On Aug 13, 1:13 pm, Shay Levi <n...@addre.ss> wrote: > The following returns all array items that equels to 3 > > PS C:\Scripts> @(1,2,3,5,3,2) -eq 3 > 3 > 3 > > How can I get the index numbers instead of the item content (i.e., 2,4)? > > Shayhttp://scriptolog.blogspot.com Someone will probably be along with a better answer in a bit, but from what I can see there is no native member of the array class that will return a collection of indicies rather than types. Ofcourse you could always just loop through the array and return each index - or build up your own collection if you need them alltogether for($i=0; $i -lt $a.length; $i++) {if($a[$i] -eq 3) {$a[$i]}} There is another option of using the [array]::IndexOf() method, this only returns the first match, but there's an overloaded signature that constricts the start and end indicies that govern the search, so you could call it iteratively. I figure by the time you've done this you've created more work than the above simple loop though. Regards, Duncan |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Array Indexes Thanks Marco & Duncan. Shay http://scriptolog.blogspot.com > On Aug 13, 1:13 pm, Shay Levi <n...@addre.ss> wrote: > >> The following returns all array items that equels to 3 >> >> PS C:\Scripts> @(1,2,3,5,3,2) -eq 3 >> 3 >> 3 >> How can I get the index numbers instead of the item content (i.e., >> 2,4)? >> >> Shayhttp://scriptolog.blogspot.com >> > Someone will probably be along with a better answer in a bit, but from > what I can see there is no native member of the array class that will > return a collection of indicies rather than types. > > Ofcourse you could always just loop through the array and return each > index - or build up your own collection if you need them alltogether > > for($i=0; $i -lt $a.length; $i++) {if($a[$i] -eq 3) {$a[$i]}} > > There is another option of using the [array]::IndexOf() method, this > only returns the first match, but there's an overloaded signature that > constricts the start and end indicies that govern the search, so you > could call it iteratively. I figure by the time you've done this > you've created more work than the above simple loop though. > > Regards, > > Duncan > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Array Indexes My two cents.. 0..(1,2,3,5,3,2).length|?{(1,2,3,5,3,2)[$_]-eq3} $a=1,2,3,5,3,2;0..$a.length|?{$a[$_]-eq3} -- Kiron |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Array Indexes Thanks guys for your answers. I filled a suggestion on the Windows PowerShell feedback page on connect, fill free to vote https://connect.microsoft.com/feedba...3137&SiteID=99 Shay http://scriptolog.blogspot.com > The following returns all array items that equels to 3 > > PS C:\Scripts> @(1,2,3,5,3,2) -eq 3 > 3 > 3 > How can I get the index numbers instead of the item content (i.e., > 2,4)? > > Shay > http://scriptolog.blogspot.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| Delete and insert indexes on second harddisk | Vista General | |||
| how to assign values to array and how to create array via variable | PowerShell | |||
| Desktop Graphics v 3D gaming performance indexes seem at odds | Vista hardware & devices | |||