locked
Find what part(number) of the array a particular line is RRS feed

  • Question

  • Hey lovely peoples, I'm wondering if this is possible?

    I have an array $table:

    Name                  Apples
    --------                ---------
    Bob                     5
    John                    6
    Jane                    5
    Tara                    12

    Now because this array is small I can see that $table[0] will be bob with 5 apples or $table[2] will be Jane with her 5.

    The goal is to translate this to an array that has thousands of entries so to find the person I run a line like

    $table | ?{$_.name -like "Jane"}

    This will return the line with Jane but how to I find out the lines number i.e. the [2] part in $table[2]?

    • Moved by Bill_Stewart Friday, July 27, 2018 8:23 PM Abandoned
    Friday, May 18, 2018 6:07 AM

Answers

  • $tbl = @(
        [pscustomobject]@{Name = 'Bob';Apples = 5},
        [pscustomobject]@{Name = 'John';Apples = 6},
        [pscustomobject]@{Name = 'Jane';Apples = 5},
        [pscustomobject]@{Name = 'Tara';Apples = 12}
    )
    $tbl.IndexOf($tbl[3])
    $x = $tbl | ?{$_.name -eq 'Jane'}
    $tbl.IndexOf($x)
    

    Don't use "like" when you mean "eq".


    \_(ツ)_/

    • Marked as answer by YogiJ Thursday, November 14, 2019 12:51 AM
    Friday, May 18, 2018 6:52 AM