in reply to Re: grep question
in thread grep question

I just assumed that grep would behave the same way as split does.
I was wrong.
perl -e'print split /param=(.*)/, "param=value"'
returns 'value', right?
Thanks

Replies are listed 'Best First'.
Re^3: grep question
by Roy Johnson (Monsignor) on May 27, 2005 at 00:41 UTC
    It does, but that's not the normal usage of split, either. You're saying that you've got a series of records separated by param=anything and that, in addition to returning the things that are separated by that, you also want to return the part in the parentheses.

    You can use the =~ operator to apply a regex to a string (which was what I showed you in my example). That is the straightforward way to do what you want. If you have a list or array of strings to apply, you can use map or foreach to set $_ to each of them in turn, and then you don't need the =~ operator, because a regex applies to $_ by default.


    Caution: Contents may have been coded under pressure.
      I was trying to clarify the confusion I had (that I just assumed that 'grep' works the same way 'split' does). My script now works with the
       'map {/re(gex)/; $1} @list'
      or
       'map {/re(gex)/ ? $1 : (); } @list'
      solution.
      I will try the regex =~ thing also. This may be faster.
      Thank you very much.