http://www.perlmonks.org?node_id=1003952


in reply to Re: How to grep exact string
in thread How to grep exact string

Hi tobyink, did you try this? i am getting syntax error if i replaced with what you gave. Thanks & Regards, Divakar

Replies are listed 'Best First'.
Re^3: How to grep exact string
by tobyink (Canon) on Nov 15, 2012 at 08:49 UTC

    My suggested grep has been correct Perl syntax since Perl 5.0. (Perhaps before?)

    Note there are no parentheses after the grep... not this:

    grep({ ... } @list)

    And note that there's no comma after the block... not this:

    grep { ... }, @list

    It just needs to be like this:

    grep { ... } @list

    If you really want parentheses, they can go on the outside:

    (grep { ... } @list)
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      I think what is breaking it for me is the $_ eq $unique. When I include that, real matches don't register as matches.

      EDIT: nevermind... typo.

Re^3: How to grep exact string
by frozenwithjoy (Priest) on Nov 15, 2012 at 08:29 UTC
    I believe the way you have it is a correct way to do it (see grep):
    grep ( /^$unique$/, @first_list_new )
    An alternate syntax is:
    grep { /^$unique$/ } @first_list_new

      That's not a search for an exact string though. $unique might contain regexp metacharacters like ".".

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Ah yes! Good point. Thanks.

        EDIT: But wait.... Your suggestion of grep { $_ eq $unique } @first_list_new isn't detecting any actual matches for me.

        EDIT #2: Nevermind... tyop. er, typo.