Here's how I would re-write this code:

#!/usr/bin/perl -w use strict; # Filter specific strings from an input file into specific files. { # Open input and output files. open ( INPUT, '<', 'html_LogFiles' ) or die "Unable to open input file: $!"; open ( BSC, '>', 'BSC' ) or die "Unable to open BSC: $!"; # Update: Sorry, got the names of these two wrong. # That's why copy and paste is bad, bad, bad. open ( SBSCSubsystem, '>', 'SBSCSubsystem' ) or die "Unable to open SBSCSubsystem: $!"; open ( MCBTSSubsystem, '>', 'MCBTSSubsystem' ) or die "Unable to open MCBTSSubsystem: $!"; # Process input lines into output files. Note that an # input line may match more than one pattern and # therefore may appear in more than one output file. while(<INPUT>) { if ( /BSC-/ ) { chomp; my $copy = $_; $copy =~ s/BSC-//; print BSC "$_ $copy\n"; } if ( /SBSCSubsystem/ ) { chomp; my $copy = $_; $copy =~ s/SBSCSubsystem//; print SBSCSubsystem "$_ $copy\n"; } if ( /MCBTSSubsystem/ ) { chomp; my $copy = $_; $copy =~ s/MCBTSSubsystem//; print MCBTSSubsystem "$_ $copy\n"; } } # Close input and output files. close ( INPUT ); close ( BSC ); close ( SBSCSubsystem ); close ( MCBTSSubsystem );

One comment at the top of the file, and three more comments after that. Your coding style adds too many comments, which means there's too much to read -- keep it simple.

If I had more time, I'd recommend a hash-based data structure to define the string you're looking for. That way, adding more filters would be a matter of adding an entry to a table, not cutting and pasting a block of code.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Updates 1406, January 5: Sorry, bungled file names of last two output files. Fixed now.


In reply to Re^10: Sorting names using Regular Expressions and placing them in different Files. by talexb
in thread Sorting names using Regular Expressions and placing them in different Files. by Kiran Kumar K V N

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.