in reply to Need help with comparison code

From as I understand your description, you want one file to be a running list of the new files that have arrived in the directory, while the second file will be a list of just the new ones at that moment. If this is the case, then the following will probably get you what you want.
#!/usr/bin/perl use strict; use warnings; # emailsent.txt-will contain the name of all files for whom an alert h +as been sent # emailtogo.txt-will contain the name of all new files for whom an ale +rt has to be sent my $matchfile = 'emailsent.txt'; my $outfile = 'emailtogo.txt'; ########################## my $directory = 'C:\'; opendir(DIR, $directory); my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR; closedir(DIR); # storing all file names in the folder in an array foreach(@files){ print $_,"\n"; } # Build list of old files for comparison open my $ih, '<', $matchfile or die "Can't open file, $matchfile: $!"; my %oldFiles = map {chomp; $_ => 1} <$ih>; close $ih; # Append new files to total list open my $oh_tot, '>>', $matchfile or die "Can't open file, $matchfile: + $!"; open my $oh_new, '>', $outfile or die "Can't open file, $outfile: $!"; # Comparing the array to the names in $matchfile foreach my $file (@files) { if (! $oldFiles{$file}) { print $oh_tot "$file\n"; print $oh_new "$file\n"; } } close $oh_tot; close $oh_new;
- Miller

Replies are listed 'Best First'.
Re^2: Need help with comparison code
by sowais (Sexton) on Feb 04, 2011 at 20:29 UTC

    Thanks alot! your code was very helpful. I had to make some minor changes but I think I'll get it working. As you know I am a beginner in Perl, could you recommed any resources where i could learn some more on how to program in Perl. I know this site is great in getting help on code already written but want to learn how to write properly first.

      The biggest and most helpful resource for any perl programmer independent of experience is perldoc. The more time you spend there and are familiar with all of the faqs and documentation, the better off you'll be.

      The second resource you'll need is cpan for all of the code provided by other perl experts for use by others.

      After that, the best resources are subjective, as there are lots of books out there and some are favored more than others depending on the programmer. If you're looking for ways to have good style, then I might suggestion Damian Conway's Perl Best Practices book. It's been out a while, but definitely stilly has some good tips.

      Good luck,

      - Miller