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

Hey guys (and gals)... not used a lot of Perl before, but I've got to maintain a piece for someone. I'm looking at a line which seems to have a fault:  if ($_ =~ /^ERROR/ && $_ =~ /$month  $day/){push @errors, $_} the line of the file it is trying to match would look like ERROR: tablename      - Description of the error Fri Oct 26 10:12:41 2007 but I don't have any matches when it's clearly there. hope you can help guys, Regards, A p.s. $month and $day are created by means of:
my $date = localtime; my @fields = split /\s+/, $date; my $year = pop @fields; my $month = $fields[1]; my $day = $fields[2];

Replies are listed 'Best First'.
Re: File line matching
by FunkyMonk (Bishop) on Oct 26, 2007 at 10:12 UTC
    There's only a single space in "Oct 26" in your test data, but two spaces in your regexp. The following prints "match":
    my $date = localtime; my @fields = split /\s+/, $date; my $year = pop @fields; my $month = $fields[1]; my $day = $fields[2]; $_ = "ERROR: tablename - Description of the error Fri Oct 26 10:1 +2:41 2007"; if ( /^ERROR/ && /$month $day/ ) { print "match\n" }

    Note that // matches against $_ by default.

Re: File line matching
by Anonymous Monk on Oct 26, 2007 at 10:11 UTC
    My most sincere apologies monks, for I have shamed myself in the monastery. I had a double space inbetween /$month and $day/. I feel silly!