in reply to grep question

I think your confusion is in what tool extracts from what. grep extracts elements from a list; regex capture extracts pieces from a string. You're using both in your example, when you only need to use a regex: perl -e 'print "param=value" =~ /param=(.*)/' On the plus side, at least nobody had to edit your post to add code tags.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: grep question
by newest_newbie (Acolyte) on May 27, 2005 at 00:29 UTC
    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
      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.