bittoni has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am new to perl and am having a problem with a filter i am trying to set up using arrays. I have a file which contains a list of values either alphanumeric or numeric. i have put all these elements into a variable to use against the original log file. It seems to work when its hard coded into the script but not when its set to the variable. Hope you can help. Below is the snippet of code thats causing problems and how it is initiated.
$filename = "/var/tmp/log.out"; open(FILE, $filename) || &error(); @file = <FILE>; close(FILE); $filter = "/var/tmp/messages_filter"; open(FILE, $filter) || &error(); @filefilter = <FILE>; close(FILE); START_HTML $lines_in_filter = @filefilter; if($lines_in_filter != 0){ foreach $filterline (@filefilter){ chomp($filterline); if ($grepstring ne "" ){ $joiner = "&&";} $grepstring = "$grepstring $joiner (!/$filterline/)"; } } else{ print "<TR><TD>File is empty</TD></TR>"; } #print "$grepstring"; #print "(!/243001/) && (!/454097/) && (!/858035/) && (!/872937/) && (! +/427199/)"; @file1 = grep($grepstring, @file); #@file1 = grep((!/243001/) && (!/454097/) && (!/858035/) && (!/872937/ +) && (!/427199/), @file); print "@file1";
The numbers listed are what are in the file, ie 243001 454097 etc... the two print lines that are commented out i put in to try and debug it and the output from the $grepstring is the same as the original output. The problem that i am having is that the
"@file1 = grep($grepstring, @file);" doesnt seem to work like the original line shown below it in the code. Please can anyone help me or point me in a better direction to try. Hope this makes sense. Thanks for your time.

Replies are listed 'Best First'.
Re: Variable problem inside an array
by Corion (Patriarch) on Apr 15, 2009 at 14:38 UTC

    You don't show us what value $grepstring has. But if the value of $grepstring looks like Perl code, it most certainly won't do what you seem to think it does.

    Maybe you can take a step back and tell us what goal you want to achieve?

      Hi Corion Sorry the output of $grepstring is
      (!/243001/) && (!/454097/) && (!/858035/) && (!/872937/) && (!/427199/)
      What i am trying to achieve is a log file filter using a text file for the filter. I am trying to output a filtered log file that doesnt display the lines/error id's that are logged in the text file, so that rather than hard coding it, we can easily add new ones to filter out by adding a line into the file. Hope this helps. Thanks for your help so far
Re: Variable problem inside an array
by jwkrahn (Abbot) on Apr 15, 2009 at 15:25 UTC

    You probably want something like this (UNTESTED):

    my $filter = '/var/tmp/messages_filter'; open FILE, '<', $filter or error(); my $grepstring = join '|', map { chomp; $_ } <FILE> or print "<TR><TD>File is empty</TD></TR>"; my $filename = '/var/tmp/log.out'; open FILE, '<', $filename or error(); my @file1; while ( <FILE> ) { next if /$grepstring/o; push @file1, $_; } print "@file1";
      Hi jwkrahn
      that works exactly as i wanted it to. Thank you so much for your help. Thanks for all the responses i have received. All knowledge is helpful to me.
Re: Variable problem inside an array
by ELISHEVA (Prior) on Apr 15, 2009 at 20:33 UTC

    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