I think the code can be further optimized without traversing through 2nd directory as below.
opendir RD,"." or die "ERROR: $!\n"; my $dir2 = "../"; my @DIR1 = readdir RD; foreach my $filename (@DIR1) { next if $filename =~/^\./; my $fname = $dir2 . $filename; if (-e $fname) { print "$filename\n" if $filename eq $_ ; } } -----Original Message----- From: Mark Goland [mailto:[EMAIL PROTECTED] Sent: Thursday, October 28, 2004 9:29 AM To: Vladimir Lemberg; [EMAIL PROTECTED] Subject: Re: Comparing two directories ----- Original Message ----- From: "Vladimir Lemberg" <[EMAIL PROTECTED]> To: "Vladimir Lemberg" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Wednesday, October 27, 2004 8:16 PM Subject: Re: Comparing two directories Hello, I beleave the two codes you have posted will yield identical results, as there are a few problems . let me go through it and comment > Actually, this is my script: > > foreach $filename(readdir DIR1) { remember this will read entire DIR1, and triverse through the list of files in created list > next if $filename =~/^\./; > while (readdir DIR2){ > print "$filename\n" if (-e $filename); > last; this will take effect first time through, which means two things 1. You will only check 1 filename from second directory each time through 2. You will check next file in DIR2 directory next time through if you want to go through this route, you will need to rewind directory handle each time you brake from this loop [ perldoc -f rewinddir ] > } > } You should probably read the entire directory contents at a time....here is example on how one can achieve your goal. #!PERl use warnings; use strict; use Data::Dumper; $|=1; my %hash; opendir RD,"." or die "ERROR: $!\n"; opendir RD2,".." or die "ERROR: $!\n"; my @DIR1 = readdir RD; my @DIR2 = readdir RD2; foreach my $filename (@DIR1) { next if $filename =~/^\./; foreach (@DIR2){ print "$filename\n" if $filename eq $_ ; } } > > > ----- Original Message ----- > From: "Vladimir Lemberg" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Wednesday, October 27, 2004 5:14 PM > Subject: Comparing two directories > > > Hi, > > > > Could you help me to find what's wrong in my script? > > > > I need to compare two directories and if file exists in both - print it. > > > > die "Usage: 2 arguments must be passed i.e. file1 file2\n" if @ARGV != 2; > opendir DIR1, $ARGV[0] or die "couldn't open $ARGV[0] for reading.\n"; > opendir DIR2, $ARGV[1] or die "couldn't open $ARGV[1] for reading.\n"; > > foreach $filename(readdir DIR1) { > next if $filename =~/^\./; > while (readdir DIR2){ > if (-e $filename){ > print "$filename\n"} > } > } > closedir(DIR1); > closedir(DIR2); > > > > Looks like I'm checking "if exists" in first directory but not in second. > > > > -Vladimir > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>