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 - Location an Array Element

Reply
 
Old 01-05-2009   #1 (permalink)
Tim Munro


 
 

Location an Array Element

Hi all,

I have an array with various bits of information, including name phone
number office location etc. If I search the array looking for a Name, how do
I retrieve the array index of that element.

For example:
$MyArray | foreach{if ($_.name -like "*tim*"){output the array index of
this element}}

Thanks.
--
Tim.



My System SpecsSystem Spec
Old 01-05-2009   #2 (permalink)
Jon


 
 

Re: Location an Array Element

You can do this by using your own array index reference 'en route' eg....

#--------------
$p = Get-Process
$p | foreach {$i=-1}{$i=$i+1;If ($_.Name -eq 'Powershell') {$i}}
#--------------



PS (1) > $p = Get-Process
PS (2) > $p | foreach {$i=-1}{$i=$i+1;If ($_.Name -eq 'Powershell') {$i}}
16
17
PS (3) > $p[16]

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
482 15 40908 34076 229 3932 powershell

--
Jon


"Tim Munro" <Excelsior@xxxxxx> wrote in message
news:edxceK0bJHA.1184@xxxxxx
Quote:

> Hi all,
>
> I have an array with various bits of information, including name phone
> number office location etc. If I search the array looking for a Name, how
> do I retrieve the array index of that element.
>
> For example:
> $MyArray | foreach{if ($_.name -like "*tim*"){output the array index of
> this element}}
>
> Thanks.
> --
> Tim.
>
My System SpecsSystem Spec
Old 01-05-2009   #3 (permalink)
Kiron


 
 

Re: Location an Array Element

# pipe available indices to Where-Object
$indices = 0..($MyArray.count-1) | ? {$MyArray[$_].name -like '*tim*'}
# these are the indices
$indices
# verify
$MyArray[$indices]

--
Kiron
My System SpecsSystem Spec
Old 01-05-2009   #4 (permalink)
Tim Munro


 
 

Re: Location an Array Element

Very slick Kiron, thanks.

Thanks to all who responded.

--
Tim.

"Kiron" <Kiron@xxxxxx> wrote in message
news:AF845E50-89EB-4C0A-B1CC-B1269C701DC0@xxxxxx
Quote:

># pipe available indices to Where-Object
> $indices = 0..($MyArray.count-1) | ? {$MyArray[$_].name -like '*tim*'}
> # these are the indices
> $indices
> # verify
> $MyArray[$indices]
>
> --
> Kiron

My System SpecsSystem Spec
Old 01-06-2009   #5 (permalink)
Josh Einstein


 
 

Re: Location an Array Element

Here's a reusable function.

function Search-List {
param ( [ScriptBlock] $Predicate )
begin { $i = 0 }
process { if ( &$Predicate ) { $i }; $i += 1 }
end { }
}

dir | Search-List { $_ -is [System.IO.DirectoryInfo] }
0
1

Josh Einstein

"Tim Munro" <Excelsior@xxxxxx> wrote in message
news:edxceK0bJHA.1184@xxxxxx
Quote:

> Hi all,
>
> I have an array with various bits of information, including name phone
> number office location etc. If I search the array looking for a Name, how
> do I retrieve the array index of that element.
>
> For example:
> $MyArray | foreach{if ($_.name -like "*tim*"){output the array index of
> this element}}
>
> Thanks.
> --
> Tim.
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
remove last element from an array PowerShell
Fast copy method of sub array (=array range) possible? VB Script
Re: error converting object to datetime array element from sql table PowerShell
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell


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