in reply to Opening multiple files in directory

Please try this...
#!/usr/bin/perl use strict; my %count; my $directory = "c:\\Test"; opendir( DIR, $directory ) || die "Unable to open directory - $!\n"; my @files = grep /\.txt/, readdir( DIR ); closedir( DIR ); open( OUTFILE, ">> $directory\\COID_LIST.txt" ) || die "Unable to open write file! - $!\n"; foreach my $file (@files) { open( FH, "$directory\\$file" ) || die "Unable to open $file - $!\n"; while( <FH> ) { my $ID = substr( $_, 260, 5 ); print OUTFILE "$ID\n"; $count{$ID}++ if ( defined( $ID ) ); } close( FH ); } print "Number of company ID = " . scalar keys %count;

Replies are listed 'Best First'.
Re^2: Opening multiple files in directory
by molson (Acolyte) on Mar 19, 2009 at 16:55 UTC
    You guys rock! Thanks for all the replies!! I used the code below and it gives me a count of the total unique company IDs, but is there a way to return a list of unique company IDs as well? Please try this...
    #!/usr/bin/perl use strict; my %count; my $directory = "c:\\Test"; opendir( DIR, $directory ) || die "Unable to open directory - $!\n"; my @files = grep /\.txt/, readdir( DIR ); closedir( DIR ); open( OUTFILE, ">> $directory\\COID_LIST.txt" ) || die "Unable to open write file! - $!\n"; foreach my $file (@files) { open( FH, "$directory\\$file" ) || die "Unable to open $file - $!\n"; while( <FH> ) { my $ID = substr( $_, 260, 5 ); print OUTFILE "$ID\n"; $count{$ID}++ if ( defined( $ID ) ); } close( FH ); } print "Number of company ID = " . scalar keys %count;
      If you stored the values in an array rather than printing them directly, you could use uniq from List::MoreUtils, which is a core module.