Your attempt to replace the grep condition with a variable fails because the first parameter to grep is supposed to be a snippet of code, not a string, and not a variable containing a string.
If you want to dynamically create the condition used with grep, you will need to construct a string containing the entire grep command and then evaluate it using eval. Here is a simple example:
my @aFruits = qw(apple bananna orange pear); #declare a variable for the grep result my @aSelected; #define your condition my $sCondition = q{/^b/ || /^p/}; #construct the grep command and evaluate it my $sEval = "\@aSelected = grep($sCondition,\@aFruits)"; eval $sEval; #see the results print "selected fruits (eval): " . join(',', @aSelected) . "\n";
Best, beth
In reply to Re: Variable problem inside an array
by ELISHEVA
in thread Variable problem inside an array
by bittoni
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |