in reply to substring search in an array of arrays

Close:

@test2 = (["KATE","1001"], ["XERXES","1002"], ["LARRY","1003"], ["TEDDY","1004"], ["DANA","1005"], ["SHERRY","1006"]);; @grepnames = grep {$_->[0] =~ "ER"} @test2;; print "@$_" for @grepnames;; XERXES 1002 SHERRY 1006

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: substring search in an array of arrays
by ikegami (Patriarch) on Feb 14, 2008 at 22:42 UTC

    "ER" is misleading. It implies ER is text while it's really a regex. Use /ER/ instead.

    Don't pretend the first argument of split is text.
    Don't pretend the RHS of =~ is text.

    my @grepnames = grep { $_->[0] =~ /ER/ } @test2; print(join(', ', @$_), "\n") for @grepnames;

    Update: Fixed LHS to say RHS.

        Sure thing. An example where the confusion leads to a difference would be $_[0] =~ "Dr.". It doesn't search for "Dr.", it searches for a three char string starting with "Dr". I mentioned split because split '|', ... is a similar common error and a reference to past threads on the topic.