Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stupid Question (but asked anyway)

Status
Not open for further replies.

mte0910

Programmer
Apr 20, 2005
232
US
Does my code below do what I want it to?
[I want to open a bunch of .csv files, and look for lines that say ACD Call or Give Default. If it says that, grab the line and place it into a new file. So the end file will contain the line ACD Call or Give Default from every one of the .csv files. The other side of this coin is that I have hundreds of files to look through and they vary in size from 1k to 50mb, but I figure the end result should be around 5mb.]

my $Line1;
my @csv_files = glob "D:/CBC/*.csv";
open (OUT,'>D:/CBC/A/TMP00.csv');
while ( $Line1 = <@csv_files>) {
chomp $Line1;
if($Line1 =~ /(ACD Call|Give Default)/){
print OUT "$Line1\n";
}
}
close (OUT);
 
Try something like this:

Code:
my $Line1;
my @csv_files = glob "D:/CBC/*.csv";
open (OUT,'>D:/CBC/A/TMP00.csv');
foreach my $file (@csv_files) {
  if(open(FH, "$file")) {
    while ( $Line1 = <FH>) {
      chomp $Line1;
      if($Line1 =~ /(ACD Call|Give Default)/){
      print OUT "$Line1\n";
    }
    close(FH);
  }
}
close (OUT);
 
You can get rid of the capturing parentheses in the regex; you aren't doing anything with the captured value. And there is no sense in chomping the data if you are going to add the newline back on after. This reduces your while loop to
Perl:
while (<FH>) {
   print OUT if (/ACD Call|Give Default/);
}
which should speed it up a little (although not much, your process is going to be I/O bound whatever happens).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top