[sf-perl] Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE NO/

Loo, Peter # PHX Peter.Loo at source.wolterskluwer.com
Mon Jan 15 15:58:46 PST 2007


Here you go:

  while (<IFH>) {
    chomp();
    my $str1 = substr($_, 0, $offset);
    my $str2 = substr($_, $offset+$length);
    my $field = substr($_, $offset, $length);
    if ($field =~ m/^\s+$/ || $field =~ m/^[0-9]+$/) {
      print OFH "$_\n";
      next;
      }
    elsif ($field =~ m/^[0-9]+\s+$/ || $field =~ m/^\s+[0-9]+$/) {
      $field =~ s/\s+//g;
      $field = sprintf("%0${length}d", $field);
      }
    elsif ($field =~ m/^[0-9]+\s+[0-9]$/) {
      $field = "";
      $field = sprintf("%${length}s", $field);
      }
    else {
      $field =~ s/$field//g;         <============ THIS IS WHERE THE
PROBLEM OCCURS.
      $field = sprintf("%${length}s", $field);
      }
    my $outStr = $str1 . $field . $str2;
    print OFH "$outStr\n";
    #print "field now is: $field\n";
    print "AFTER:  $outStr\n\n";
    }

 
Peter

-----Original Message-----
From: sanfrancisco-pm-bounces+peter.loo=source.wolterskluwer.com at pm.org
[mailto:sanfrancisco-pm-bounces+peter.loo=source.wolterskluwer.com at pm.or
g] On Behalf Of David Fetter
Sent: Monday, January 15, 2007 4:53 PM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] Unmatched [ in regex;marked by <-- HERE in m/[
<-- HERE NO/

On Mon, Jan 15, 2007 at 04:31:11PM -0700, Loo, Peter # PHX wrote:
> 
>    Hello,

Hi

>    I am getting the following error when I hit the following record
when trying
>    to m// the field "[NO".
> 
>    my $field = substr($_, $offset, $length);
> 
>    elsif ($field =~ m/^[0-9]*\s+[0-9]$/ || $field =~ m/\W+/) {
                      ^^^^^^^^^^^^^^^^^^^ This is better written as

    $field =~ m(
                \A    # Start
                \d*   # 0 or more digits
                \s+   # some whitespace
                \d    # a digit
                \z    # End
               )mx

> When I leave in the part "|| $field =~ m/\W+/)" the error goes away, 
> but without this, I am getting an error.

It would help a lot if you sent a self-contained example that we could
test :)

Cheers,
D (who just hates working too hard)
--
David Fetter <david at fetter.org> http://fetter.org/
phone: +1 415 235 3778        AIM: dfetter666
                              Skype: davidfetter

Remember to vote!
_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org
http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


This E-mail message is for the sole use of the intended recipient(s) and
may contain confidential and privileged information.  Any unauthorized
review, use, disclosure or distribution is prohibited.  If you are not
the intended recipient, please contact the sender by reply E-mail, and
destroy all copies of the original message.


More information about the SanFrancisco-pm mailing list