![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | Re: Position in an array Thanks. Your solution gets it done too! -- Kiron |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Position in an array Thanks. Your solution gets it done too! Nice concurrent posts by the way. -- Kiron |
My System Specs![]() |
![]() |
| 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 | |||