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

I'm having a couple problems with the following. I can't seem to get it to replace the * with !. It leaves the output unchanged.
my $output; while( my $line = <DATA> ) { # get rid of line terminator: chomp $line; # Get rid of line numbers: $line =~ s/^N[0-9]{2}\ //; # Replace * and the beginning of the line with !: $line =~ s/^\*(.*)/!$1/ and print $line and next if $line; # Keep any numbers prefixed with X,Y or Z.: $line =~ s/(X|Y|Z)([0-9.-]+)/ $2/g if $line; # Throw the rest away: $line =~ s/[A-Z][0-9.+-]+//g if $line; open print $line; } __DATA__ O1407!SILH 5B NECK PROFILE STD.> N1! 11-14-01 N2!T1 3/4" ENDMILL N3! OUTPUT IN ABSOLUTE N4L0200 N5! SUB NUMBER: 2 N6G90G0X11.108Y-18.25 N7G1Z-1.13F80.
Also, when I try to read the data in from a file..ie:
$file = "file.txt"; open (FILE, "$file"); my $output; while( my $line = <FILE> )... etc.
I don't even get close to the desired output as in the first snippet. I know this is probably an easy fix for the almighty monks...I just can't seem to find what's wrong. Thank you for any help!

Replies are listed 'Best First'.
Re: shouldn't this work?
by Cine (Friar) on Jul 22, 2002 at 17:36 UTC
    There are no stars in your input! And your script should work fine if there were.
    With a little code cleanup it becomes:
    my $output; while( my $line = <DATA> ) { # get rid of line terminator: chomp $line; next unless $line; # Get rid of line numbers: $line =~ s/^N[0-9]{2}\ //; # Replace * and the beginning of the line with !: $line =~ s/^\*/!/ and print $line and next; # Keep any numbers prefixed with X,Y or Z.: $line =~ s/(X|Y|Z)([0-9.-]+)/ $2/g; # Throw the rest away: $line =~ s/[A-Z][0-9.+-]+//g; print $line; } ...


    T I M T O W T D I
Re: shouldn't this work?
by broquaint (Abbot) on Jul 22, 2002 at 17:40 UTC
    Change this line
    $line =~ s/^\*(.*)/!$1/ and print $line and next if $line;
    to
    print("!" . substr($line, 1)) and next if index($line, '*') == 0;
    And what purpose does the if modifier serve after some of your substitutions (and I guess the spurious open is a mis-paste)? I wonder what was wrong with the code examples in your previous node?
    HTH

    _________
    broquaint