![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||