Here is a different idea for you... Instead of if/else, I used a "dispatch table" to take action on the various op codes. The default is to just do nothing by printing the existing line. If the op code has an action, then it is done (that might be "nothing" in the case of G92 or M190)

#!usr/bin/perl use warnings; use strict; $|=1; #turn off stdout buffering, easier to debug warnings... my %dispatch = (G92 => sub{return}, G10 => sub{print "M104 S0 ; laser off\n";}, G11 => sub{print "M400 ; wait for moves to finish\n"; print "M104 S100 ; laser on\n";}, M190 => sub{return}, G1 => \&G1, ); while (my $line = <DATA>) { chomp $line; my ($op,$rest) = split ' ',$line,2; $rest //= ''; #define $rest as null string if undefined if (exists $dispatch{$op}) { $dispatch{$op}->($rest); } else { print "$line\n"; } } sub G1 { my $rest = shift; $rest =~ s/Z/Z-/; $rest =~ s/E[\d.]+//; print "G1 $rest\n"; } =prints: T1 G1 Z-0.050 F7800.000 M104 S0 ; laser off G1 Z-0.150 F7800.000 G1 X14.725 Y-8.975 F7800.000 G1 Z-0.050 F7800.000 M400 ; wait for moves to finish M104 S100 ; laser on G1 X14.725 Y8.975 F120.000 M104 S0 ; laser off G1 Z-0.150 F7800.000 G1 X-9.585 Y-0.615 F7800.000 G1 Z-0.050 F7800.000 M400 ; wait for moves to finish M104 S100 ; laser on =cut __DATA__ G92 E0 ; want to remove all G92 lines (works now) T1 G1 Z0.050 F7800.000 G10 ; retract G92 E0 G1 Z0.150 F7800.000 G1 X14.725 Y-8.975 F7800.000 G1 Z0.050 F7800.000 G11 ; unretract G92 E0 G1 X14.725 Y8.975 E0.01465 F120.000 G10 ; retract G92 E0 G1 Z0.150 F7800.000 G1 X-9.585 Y-0.615 F7800.000 G1 Z0.050 F7800.000 G11 ; unretract G92 E0

In reply to Re: How to search and replace text characters in a perl script by Marshall
in thread How to search and replace text characters in a perl script by thetekguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.