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 - Position in an array

Reply
 
Old 09-12-2007   #1 (permalink)
bugiwan


 
 

Position in an array

I have an array that contains numbers 1 through 10 in a random order: (4, 2,
8, 7, 1, 9, 10, 3, 5, 6). I would like to know if there is a function that I
can call that will take an input of an element in the array and return its
position.

FunctionName(5) would return 9 since 5 is the 9th element of the array.

Thanks for your help.


My System SpecsSystem Spec
Old 09-12-2007   #2 (permalink)
ebgreen


 
 

RE: Position in an array

My solution to your problem would be to use an arraylist instead:


[System.Collections.ArrayList]$foo = 4, 2, 8, 7, 1, 9, 10, 3, 5, 6
$foo.IndexOf(5)

"bugiwan" wrote:
Quote:

> I have an array that contains numbers 1 through 10 in a random order: (4, 2,
> 8, 7, 1, 9, 10, 3, 5, 6). I would like to know if there is a function that I
> can call that will take an input of an element in the array and return its
> position.
>
> FunctionName(5) would return 9 since 5 is the 9th element of the array.
>
> Thanks for your help.
>
My System SpecsSystem Spec
Old 09-12-2007   #3 (permalink)
Kiron


 
 

Re: Position in an array

In PowerShell arrays are zero based. The first element's index is 0.

$arr = 4, 2, 8, 7, 1, 9, 10, 3, 5, 6

# You can call the IndexOf Static Method

[array]::indexOf($arr, 5)

# ... or create a function:

function IndexOf ([object[]]$array, $element)
{
0..($array.length - 1) | where {$array[$_] -eq $element}
}

IndexOf $arr 5

--
Kiron

My System SpecsSystem Spec
Old 09-12-2007   #4 (permalink)
ebgreen


 
 

Re: Position in an array

Nice. It did seem silly that the array object would not have that
functionality but a quick search didn't turn it up and I knew that arraylist
did so I suggested that.

"Kiron" wrote:
Quote:

> In PowerShell arrays are zero based. The first element's index is 0.
>
> $arr = 4, 2, 8, 7, 1, 9, 10, 3, 5, 6
>
> # You can call the IndexOf Static Method
>
> [array]::indexOf($arr, 5)
>
> # ... or create a function:
>
> function IndexOf ([object[]]$array, $element)
> {
> 0..($array.length - 1) | where {$array[$_] -eq $element}
> }
>
> IndexOf $arr 5
>
> --
> Kiron
>
My System SpecsSystem Spec
Old 09-12-2007   #5 (permalink)
Kiron


 
 

Re: Position in an array

Thanks. Your solution gets it done too!
--
Kiron

My System SpecsSystem Spec
Old 09-12-2007   #6 (permalink)
Kiron


 
 

Re: Position in an array

Thanks. Your solution gets it done too!
Nice concurrent posts by the way.

--
Kiron

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Fast copy method of sub array (=array range) possible? VB Script
How to create array without quotes? $array = (a,b,c) PowerShell
Array indexing: Want to say "Item #2 through the rest of the array." PowerShell
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
how to assign values to array and how to create array via variable 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