in reply to grep question

Why grep? The purpose of grep is to filter a list according to some criterion, which doesn't sound at all like what you want to do. Of course, if one is hellbent on using grep for this, Perl will oblige with WTDIs galore, but this hardly qualifies as an argument in favor of using grep here.

the lowliest monk

Replies are listed 'Best First'.
Re^2: grep question
by fishbot_v2 (Chaplain) on May 26, 2005 at 22:33 UTC

    Of course, if one is hellbent on using grep for this, Perl will oblige with WTDIs galore...

    One such solution:

    perl -e 'print grep { s/param=// } @{[ "param=value" ]};'

    ...but this hardly qualifies as an argument in favor of using grep here

    Agreed. Other than as a proof-of-concept, don't do this. I'd personally use map for the OPs exact stated desire. I suspect, though, if the OP gave the larger context of the problem, better solutions might be apparent.

      Personally, I'd use both map and grep for what the OP is doing:

      return map { /memtype=(.*)/i; $1 } grep /memtype=/, $self -> collect_d +ata ('all');
      I realise that this probably doesn't speed things up, maybe even slows it down. But it does what it says much more obviously to me. I'm collecting all the data. Then I'm extracting just the ones I want (memtype), and then I'm grabbing the stuff on the right of the equals sign. Just seems more straightforward to me. As for brian d foy's concern about using $1 when you haven't checked if it matched, I hope this is ok - I agree with him, and I think this is ok. ;-)