Hi. Please may we have a name, at least in the sig? Something
in me rebels against writing 'Hi Lonewolf'.


<[EMAIL PROTECTED]> wrote:
>
> I have to parse a big file and I do it line by line to keep stuff going
> correctly.  I am at a point where I need it to go through and replace every
> " with inches and ' with feet, IF item in front of it is a digit (0-9).
>
> I have this written in the script to parse it:
>         while ($line = <OLDFILE>)  {
> #               $line = $line =~ /^\s*(.*)\s*\n$/;
> $line =~ s/^ //;
> $line =~ s/ $//;
> $line =~ s/\t/|/g;
> $line =~ s/\s+/ /mg;
> $line =~ s/^\s*//mg;
> $line =~ s/\s*$//mg;
> $line =~ s/\s*$//mg;
> ###  The following lines mod the files to reflect inches and feet
> $line =~ s/"/in./mg;
> $line =~ s/'/ft./mg;
>                 print NEWFILE "$line\n";
>
> help!

Your first regex deletes a single leading space character while
your fifth deletes all leading whitespace. So the first is
unnecessary.

Your second regex deletes a single trailing space character while
your seventh and eighth delete all trialing whitespace. So two of
these are unnecessary unnecessary.

It would be a lot tidier to use the default $_ variable in this
loop. It will also correct the slightly fussy problem that, as
it is, the loop will exit without processing an empty trailing
record with no terminator.

Finally, most of the /g and all of the /m modifiers have no effect.
(Unless you've played with $/ to slurp your entire file?)

Try this:

  while (<OLDFILE>)  {

    s/\t/|/g;   # change tab separators to pipes

    s/^\s+//;   # remove all leading space
    s/\s+$//;   # and trailing space
    s/\s+/ /g;  # and compress multiple spaces

    # The following lines mod the files to reflect inches and feet
    #
    s/(?<=\d)"/in./g;
    s/(?<=\d)'/ft./g;

    print NEWFILE "$_\n";
  }

HTH,

Rob





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to