Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Array Indexes

Reply
 
Old 08-13-2007   #1 (permalink)
Shay Levi
Guest


 
 

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 SpecsSystem Spec
Old 08-13-2007   #2 (permalink)
Marco Shaw
Guest


 
 

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 SpecsSystem Spec
Old 08-13-2007   #3 (permalink)
Duncan Smith
Guest


 
 

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 SpecsSystem Spec
Old 08-13-2007   #4 (permalink)
Shay Levi
Guest


 
 

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 SpecsSystem Spec
Old 08-14-2007   #5 (permalink)
Kiron
Guest


 
 

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 SpecsSystem Spec
Old 08-14-2007   #6 (permalink)
Shay Levi
Guest


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46