Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageActually, this is my script:
foreach $filename(readdir DIR1) {
next if $filename =~/^\./;
while (readdir DIR2){
print "$filename\n" if (-e $filename);
last;
}
}
----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: <beginners@perl.org>
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
Post Follow-up to this message
----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>; <beginners@perl.org>
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" <lemba@sbcglobal.net>
> To: <beginners@perl.org>
> 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
Post Follow-up to this messageI 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:mgoland@optonline.net]
Sent: Thursday, October 28, 2004 9:29 AM
To: Vladimir Lemberg; beginners@perl.org
Subject: Re: Comparing two directories
----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>; <beginners@perl.org>
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" <lemba@sbcglobal.net>
> To: <beginners@perl.org>
> 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Post Follow-up to this messageVladimir Lemberg wrote:
> Hi,
Hello,
> 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.
No, you are checking "if exists" in neither directory.
die "Usage: 2 arguments must be passed i.e. dir1 dir2\n" if @ARGV != 2;
opendir DIR1, $ARGV[0] or die "couldn't open $ARGV[0] for reading.\n";
foreach my $filename ( readdir DIR1 ) {
next if $filename =~ /^\./;
if ( -e "$ARGV[1]/$filename" ) {
print "$filename\n"}
}
}
closedir DIR1;
John
--
use Perl;
program
fulfillment
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.