1

I have a table $t (an imported csv) with several columns. The first header is 'A'. I want to know the number (index) of the row where the value in column 'A' equals the searchstring $s (without using a loop).

So I found [array]::indexOf(<1-dimensional-array>,<searchString>) In order to use this, I need to create a 1-dimensional array from column 'A'. I tried $a = @($t | select A) and then I can address each element like $a[0] or $a[3]. So does this qualify as an one-dimensional-array in the context of indexOf? When I set the search string $s to a value that is present, then [array]::indexOf($a,$s) always returns -1.

jamacoe
  • 519
  • 4
  • 16
  • 1
    In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the [linked answer](https://stackoverflow.com/a/48809321/45375) for details and alternatives. – mklement0 Aug 01 '21 at 22:10
  • 1
    As an aside: The `IndexOf()` method is case-_sensitive_, whereas PowerShell's native features are by default case-_insensitive_. – mklement0 Aug 01 '21 at 22:11
  • Thank you, that worked right away: $a = @($t | select -expandProperty A). As an aside: what would a native Powershell feature be for this task ( [array]::indexOf ) ? - Ok, got .LastIndexOf() – jamacoe Aug 01 '21 at 22:19
  • 1
    Glad to hear it. Unfortunately, there are currently no PowerShell-native equivalents to `IndexOf()` / `LastIndexOf()` – mklement0 Aug 01 '21 at 22:25

0 Answers0