I have a script that goes thru an input file (always named cpuf.rpt) and does some formatting on it. The problem is that the file is "formatted" already by some wannabe's old utility, so I have to unformat it first, then put it into the format I need for (my) generic reporting utility.
#!/usr/perl/bin -w my @coswork_test; my @func_test; my $count = 0; open (REPORT, "cpuf.rpt") || die "Can't open CPUF.RPT: $!\n"; open (OUTFILE, ">cpuf.med") || die "Can't open CPUF.OUT: $!\n"; while (<REPORT>) { s/^ +//; #remove the leading whitespace; s/\r//; #remove all carriage returns; s/ +$//; #remove the trailing whitespace; s/.+ : //; #remove the header information; if ($_ =~ /\*+/) { $_ =~ s/\*+//; #remove asterisk only rows; $count++; #the asterisk line only happens once per recor +d, } #so we can use it to count records. s/\n/³/; if (($_ =~ /Cosmetics/)||($_ =~ /Function/)) { #look for _X_Pass or + _X_Fail $_ =~ s/.+_X_(.{4}).*/$1/; #make it Pass or Fail } print OUTFILE "$_"; #print the line to the intermediate file. } close OUTFILE; open (INFILE, "cpuf.med") || die "Can't open CPUF.MED $!\n"; open (OUTFILE, ">cpuf.out") || die "Can't open CPUF.OUT $!\n"; #now, print the headers: print OUTFILE "DATE³QC SPEC.³REPAIR NUM³TECHNICIAN³CPU MODEL³CPU ASSET + NUM³COSMETICS ". "WORKMANSHIP TEST³FUNCTIONALITY TEST³FAILURE DETAIL\n"; #and the information: while (<INFILE>) { s/.{129}//; s/³{2}/\n/g; #replace ³PassFail³ with ³Pass³Fail³, and Fail with Fail³³ s/³Fail³/³Fail³³/g; s/³PassFail³/³Pass³Fail³/g; s/Failure Detail - //g; print OUTFILE "$_"; }
As you can see, it's a veritable feast of regex. What I would like to know is how I can combine some of these substitutions and create code that is (maybe) a bit more self documenting.
Two goals here - I'm a lazy typist and I don't want to forget what it was supposed to do when I come back to it in a year....
Simplicus

In reply to clearer code by Simplicus

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.