[Pdx-pm] anonymous (?) regex use

Ben Prew ben.prew at gmail.com
Thu Jan 13 10:26:04 PST 2005


m// is an operator.  By putting "/fu/" in a string, it no longer sees
// as an operator.  Rather it sees it as part of a string.

You want to do this:

my $re = 'fu';

grep { /$re/ } @group

or, more explicitely,

grep { m/$re/ } @group

Where m// is an operator, similar to + or *.  It works for the same
reason you can't do this:

my $sum = "3 + 4";

print $sum;

and expect to get 7. (Of course, you could print eval $sum, and get 7,
but that's not the point)

Hope that helps

On Wed, 12 Jan 2005 12:04:44 -0800, Michael Rasmussen <mikeraz at patch.com> wrote:
> I'm trying to build a regex on the fly and then search an array:
> 
>   #!/usr/bin/perl -Tw
> 
>   @group = qw ( foo bar baz fuz mlr );
>   $regex = "/fu/";
> 
>   @found = grep {$regex} @group;
> 
>   foreach (@found) {
>     print "$_\n";
>   }
> 
> but this populates @found with all members of @group.
> When I change the grep line to:
>   @found = grep {/fu/} @group;
> then @found is populated with one element, fuz.
> 
> Cluestick anyone?
> 
> --
>     Michael Rasmussen, Portland Oregon
>   Be appropriate && Follow your curiosity
>  http://meme.patch.com/memes/BicycleRiding
>    Get Fixed:  http://www.dampfixie.org
>   The fortune cookie says:
> Is it NOUVELLE CUISINE when 3 olives are struggling with a scallop in a
> plate of SAUCE MORNAY?
> 
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
> 


-- 
Ben Prew
ben.prew at gmail.com


More information about the Pdx-pm-list mailing list