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

update, ca 5 mins after posting: bmann posted correct answer just about same time as tye was helping me see my error on the cb. Thanks all! (solution: s/\Q$delete_candidate\E//)

This started out as demo-answer to a node now-deleted (because it was near identical dupe of another, [id://#548211], which has data slightly different).

But then, ka-ching!, AS 5.8.6 on W2K spat an error I did not expect, to wit: "Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE ...

Well, as you can see in code below, that's not exactly what's in the regex, but it's sorta' close to what's in line 7. BTW: data in this code is as OP posted, except this has more lines and varying years.

#! /usr/bin/perl -w use strict; use vars qw ( $string_in_file $delete_candidate @data_out $data_out ); foreach (<DATA> ) { $string_in_file = $_; # get the latest line from the file to the v +ar if ( $string_in_file =~ /^(\*{4}200[^6].*)$/ ) { # Quantifier follows nothing in regex; marked by <-- HERE in m/* <- +- HERE $delete_candidate = $1; s/$delete_candidate//; } else { push @data_out, $string_in_file; } } for $data_out(@data_out) { print $data_out; } __DATA__ ****2000-01-01****test1*****12345***** ****2001-01-01****test1*****12345***** ****2002-01-01****test1*****12345***** ****2003-01-01****test1*****12345***** ****2004-11-19****test1*****12345***** ****2005-07-01****test1*****12345***** ****2006-05-09****test1*****12345*****

the asterisk appears to me to be properly escaped; AFICT, Mastering Regular Expressions supports that...

So does this occur in other distributions? I doubt AS cares about correcting an older v. unless this is generic... and then only if I haven't contributed an error of my own that explains this.

CAUTION: see bmann below. full error message - to which I should have paid more careful heed - correctly IDed error line thusly: ... at date_elim.pl line 10, <DATA> line 7. <DATA> line 7; not code line 7.
AND see ikegami's re the other badness of the code above... His points are generally well-taken -- esp re my illogic in "throwing away $_" when my intent was to show the other poster that the first regex was a straightforward way to solve his problem.

Replies are listed 'Best First'.
Re: regex error puzzle
by ikegami (Patriarch) on May 09, 2006 at 17:49 UTC
    You're looking at the wrong line. The error occurs in s/$delete_candidate//;. Perhaps you want s/\Q$delete_candidate//;?

    Update:

    Don't use foreach (<FILE>) (which loads the entire file into memory) when while (<DATA>) (which reads the file one line at a time) suffices.

    Also, your code is needlessly complicated. In fact, the regular expession that was giving you problems is completely useless. (It's used to modify $_ which is promptly discarded.) Your code simplifies to the following:

    # If you need the stuff in @data_out my @data_out; while (<DATA>) { next if /^\*{4}200[^6]/; push @data_out, $_; } print @data_out;
    or
    # If you don't need @data_out while (<DATA>) { print unless /^\*{4}200[^6]/; }
    or
    # If you want something easy to read and # don't mind loading the entire file into memory. print grep { !/^\*{4}200[^6]/ } <DATA>;
Re: regex error puzzle
by bmann (Priest) on May 09, 2006 at 17:48 UTC
    It's actually the next regex that perl is complaining about.

    Try changing the second to s/\Q$delete_candidate//; in order to escape the asterisks.

    Update: Recounted line numbers... the regex in question is on line 10.

    Update 2: Originally thought the error gave the line of the if statement... tye posted a correction while I was posting the first update.

      perl reports any error on line 10, the first line of the if block

      No, line 10 isn't the first line of the 'if' block. Perl does report misleading line numbers for some errors in 'if' blocks, but this is restricted to saying that errors in an 'elsif' conditional expression occurred on the line of the matching 'if'.

      - tye