Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^8: Sorting names using Regular Expressions and placing them in different Files.

by g0n (Priest)
on Jan 03, 2007 at 17:13 UTC ( #592801=note: print w/ replies, xml ) Need Help??


in reply to Re^7: Sorting names using Regular Expressions and placing them in different Files.
in thread Sorting names using Regular Expressions and placing them in different Files.

Try working up a very basic case to narrow down the functionality you are having trouble with. Something like this:

use strict; use warnings; my $var = "-rw-rw-rw- 1 bsmbin bsmbin 303 Dec 4 23:00 BSC-200412041100 +00 -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 4 23:00 20041204110000"; my ($transformed) = $var=~s/BSC-//; print $var . " " . $transformed . "\n";

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".


Comment on Re^8: Sorting names using Regular Expressions and placing them in different Files.
Download Code
Re^9: Sorting names using Regular Expressions and placing them in different Files.
by Kiran Kumar K V N on Jan 05, 2007 at 17:30 UTC
    #!/usr/bin/perl -w use strict; use warnings; ## Open I/P File "bsm1_LogFiles" for reading. open(INPUT,'<','bsm1_LogFiles') or die $!; ## Read from the Input File. my $line = <INPUT>; ## Open the O/P File "BSC" for writing. open(BSC,'>','BSC') or die $!; ## Open the O/P File "SBSCSubsystem" for writing. open(SBSCSubsystem,'>','SBSCSubsystem') or die $!; ## Open the O/P File "MCBTSSubsystem" for writing. open(MCBTSSubsystem,'>','MCBTSSubsystem') or die $!; ## Search each Line in the Input File. for my $searchline(<INPUT>) { ## Search for parameter 'BSC-'. if( $searchline =~ /BSC-/) { ## Read a line,copy it,& delete 'BSC-' in copy. chomp; my $copy = $_; $copy =~ s/BSC-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'BSC'. my $line = 'BSC'; print BSC $searchline; } ## Search for parameter 'SBSCSubsystem-'. if( $searchline =~ /SBSCSubsystem-/) { ## Read a line,copy it,& delete 'SBSCSubsystem-' in + copy. chomp; my $copy = $_; $copy =~ s/SBSCSubsystem-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'SBSCSubsystem'. my $line = 'SBSCSubsystem'; print SBSCSubsystem $searchline; } ## Search for parameter 'MCBTSSubsystem-'. if( $searchline =~ /MCBTSSubsystem-/) { ## Read a line,copy it,& delete 'MCBTSSubsystem-' in c +opy. chomp; my $copy = $_; $copy =~ s/MCBTSSubsystem-//; ## Print the original line and the copy. print "$_ $copy\n"; ## Write to the Output File 'MCBTSSubsystem'. my $line = 'MCBTSSubsystem'; print MCBTSSubsystem $searchline; } } ## Close the O/P File "BSC"; close (BSC) or die $!; ## Close the O/P File "SBSCSubsystem"; close (SBSCSubsystem) or die $!; ## Close the O/P File "MCBTSSubsystem"; close (MCBTSSubsystem) or die $!;

      The above is giving the following compile Errors :- 1) Use of uninitialized value in scalar chomp at Data1.pl line 62, <INPUT> line 5310. 2) Use of uninitialized value in substitution (s///) at Data1.pl line 64, <INPUT> line 5310. 3) Use of uninitialized value in concatenation (.) or string at Data1.pl line 67, <INPUT> line 5310. Please help me in resolving this to get correct O/P.

        Line 62: use chomp $searchline instead of just chomp.
        Line 64: use $searchline instead of $_ on the previous line.
        Line 67: use $searchline instead of $_.

        ---
        It's all fine and dandy until someone has to look at the code.

      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.

        Hi All,

        #!/usr/bin/perl -w use strict; use warnings; ## Open I/P File "bsm1_LogFiles" for reading. open(INPUT,'<','bsm1_LogFiles') or die $!; ## Read from the Input File. my $line = <INPUT>; ## Open the O/P File "BSC" for writing. open(BSC,'>','BSC') or die $!; ## Open the O/P File "SBSCSubsystem" for writing. open(SBSCSubsystem,'>','SBSCSubsystem') or die $!; ## Open the O/P File "MCBTSSubsystem" for writing. open(MCBTSSubsystem,'>','MCBTSSubsystem') or die $!; ## Process input lines into output files. while(<INPUT>) { ## For parameter 'BSC-'. if ( /BSC-/ ) { ## Read a line, copy it, and delete 'BSC-' in the copy. chomp $line; my $copy = $_; $copy =~ s/BSC-//; ## Print the original line and the copy. print BSC "$_ $copy\n"; } ## For parameter 'SBSCSubsystem-'. if ( /SBSCSubsystem-/ ) { ## Read a line, copy it, and delete 'SBSCSubsystem-' in the cop +y. chomp $line; my $copy = $_; $copy =~ s/SBSCSubsystem-//; ## Print the original line and the copy. print SBSCSubsystem "$_ $copy\n"; } ## For parameter 'MCBTSSubsystem-'. if ( /MCBTSSubsystem-/ ) { ## Read a line, copy it, and delete 'MCBTSSubsystem-' in the co +py. chomp $line; my $copy = $_; $copy =~ s/MCBTSSubsystem-//; ## Print the original line and the copy. print MCBTSSubsystem "$_ $copy\n"; } } ## Close the I/P File "bsm1_LogFiles"; close (INPUT) or die $!; ## Close the O/P File "BSC"; close (BSC) or die $!; ## Close the O/P File "SBSCSubsystem"; close (SBSCSubsystem) or die $!; ## Close the O/P File "MCBTSSubsystem"; close (MCBTSSubsystem) or die $!;

        1) The above code gives the one of the Output to the File BSC as :-

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-20041202143000

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 20041202143000

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-20041202140000

        -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 20041202140000

        2) What if, I need to get the output to the File BSC as just:-

        BSC-20041202143000 20041202143000

        BSC-20041202140000 20041202140000

        Similar Output for other 2 Files SBSCSubsystem and MCBTSSubsystem. Please let me know.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://592801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2015-07-04 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls