in reply to Opening multiple files in directory

Since others have answered the question you asked, I thought I would answer one that you didn't.<grin/>

Often the simplest way to process more than one file is to pass those files to your script on the command line. Then you can use one of Perl's built-in features to make your life easier. In the code below, I've removed the open() call for the read file and changed your while loop.

#!/user/bin/perl open (OUTFILE, '>> COID_LIST.txt') or die "Unable to open Write File!\ +n$!"; while (<>) { my $COMPANY_ID = substr $_, 260, 5; push @COMPANIES, {comp => $COMPANY_ID}; } foreach (@COMPANIES) { $sum{$_->{'comp'}} += $_->{'count'}; } #Output in %sum use Data::Dumper; print OUTFILE Dumper(\%sum); close OUTFILE;

The resulting code is called with the list of files to process on the command line. The code will then process the files from the command line, one at a time until complete. This approach can simplify some problems.

G. Wade

Replies are listed 'Best First'.
Re^2: Opening multiple files in directory
by Anonymous Monk on Aug 31, 2014 at 16:17 UTC

    Just wanted to say thanks this helped me a lot making this:

    grepcertainfilesgrepcertainlinesandputresultsinfiles.pl

    #!/usr/bin/perl use strict; use warnings; # FIND THE PATH TO THE Directory: my $dir = $ARGV[0]; opendir(DIR, $dir) or die $!; # select for files with names containing txt.fit my @files= grep { /txt.fit/ } readdir DIR; closedir DIR; # Read files line by line foreach my $file(@files) { open IN, "<$file" or die $!; my $tree = (); while(<IN>) { # skip everything in file not conatining Tree mixture next unless ($_ =~ m/Tree mixture/); # remove content from line thats unwanted in output $tree = $_; $tree =~s/Tree mixtureTree=//; } #name output file after input file but add .tre my $outfile = "$file.tre"; # here i 'open' the file, saying i want to write to it with the '>>' s +ymbol open (FILE, ">> $outfile") || die "problem opening $outfile\n"; print FILE $tree; }

      I haven’t tried to run this code, but just looking through it there are two obvious problems:

      1. Within a regex, a dot matches any character other than a newline (unless the regex has an /s modifier). So /txt.fit/ matches “mytxt.fit”, but it also matches “yourtxtafit”, “histxt0fitandmore”, etc. You need to backslash the . to make it match a literal dot only: /txt\.fit/; and unless “fit” may be followed by other characters, you want: /txt\.fit$/.

      2. $tree is a scalar variable. Within the inner while loop, each time the line $tree = $_; is executed, it overwrites whatever was in the variable. So when this inner loop finishes, only the last line containing “Tree mixture” is written to the output file. Here is one way to correct this (untested):

        foreach my $file (@files) { my $outfile = "$file.tre"; open(IN, '<', $file) or die $!; open(FILE, '>>', $outfile) or die $!; while (my $tree = <IN>) { next unless /Tree mixture/; $tree =~ s/Tree mixtureTree=//; print FILE $tree; } close FILE or die $!; close IN or die $!; }

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,