Simplicus has asked for the wisdom of the Perl Monks concerning the following question:
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.#!/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 "$_"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: clearer code
by stephen (Priest) on Apr 18, 2000 at 04:43 UTC | |
by Simplicus (Monk) on Apr 19, 2000 at 16:50 UTC | |
|
RE: clearer code
by chromatic (Archbishop) on Apr 18, 2000 at 01:40 UTC | |
by Simplicus (Monk) on Apr 19, 2000 at 16:45 UTC | |
by chromatic (Archbishop) on Apr 19, 2000 at 20:02 UTC | |
by btrott (Parson) on Apr 19, 2000 at 20:34 UTC | |
|
Re: clearer code
by pschoonveld (Pilgrim) on Apr 18, 2000 at 00:52 UTC | |
by Simplicus (Monk) on Apr 19, 2000 at 16:43 UTC |