in reply to Re^2: modify list through grep
in thread modify list through grep

Yes, you're right. My reply was a bit tongue-in-cheek - the OP asked why his grep aliasing example "didn't work" and I resolved to beat it into submission ;)

There's a slight difference though. *m = \@l; aliases the entire array object. The sub {\@_} trick creates a new array object containing scalar elements which are aliased to memberes in @l matching /a/ (in OP's example it contains only one element aliased to the first element in @l - "a").

Replies are listed 'Best First'.
Re^4: modify list through grep
by syntactic (Initiate) on Nov 26, 2006 at 21:30 UTC
    thanks, this would be a solution for me, unfortunately the code with sub still gives "a b c "
    Extended version of the code I'm trying to get to work
    foreach $p ( @patterns ) { if ( ??? grep /$p/, @values ) { modify the matching list value; } else { push @values, "strings $pattern"; } }
    For the modification part I can use grep again and modify the value through $_, but then I call expensive grep twice. If I could save a reference (alias?) into @values in the if condition statement I can have a lighter code.
      for my $p ( @patterns ) { my $found= 0; for my $last ( ( grep /$p/, @values )[-1] ) { $last .= '.'; # whatever mod you want $found= 1; } push @values, "string $p" if ! $found; }

      - tye