LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems in using DIFF in PERL (http://www.linuxquestions.org/questions/linux-newbie-8/problems-in-using-diff-in-perl-761918/)

Phred_42 10-14-2009 03:46 PM

Problems in using DIFF in PERL
 
I'm learning PERL and writing production code at the same time, not fun. I am currently comparing directories using "diff -r file1 file2" In a manual mode, this works great. but not so good when it is installed into a PERL program -

----------------------------------------------------------------
$DiffLocation="/DIR1/Parent/";

print "Name of listing: ";
my $DataFile=<STDIN>;
chomp $DataFile;

my $DataName=$DiffLocation.$DataFile."\.txt";
my $DiffName=$DiffLocation.$DataFile."_Output\.txt";


open(DATANAME,"<$DataName") or die("Unable to open $DataName \n");
open(DIFFNAME,">$DiffName") or die("Unable to open $DiffName \n");


while (<DATANAME>)
{
my $DataList=$_;
my @DataString=split(/\t/,$DataList);
chomp @DataString;

print "Comparing entries $DataString[0]: $DataString[1] and $DataString[2] \n";
print DIFFNAME $DataString[1] $DataString[2];
`diff -r $DataString[1] $DataString[2] >> $DiffName`;

}
close(DIFFNAME);
close(DATANAME);

-----------------------------------------------------------------

What I get for a typical data set consisting of
Order : Dir Path 1 : Dir Path 2
------------------------------------------------------------
1 : /DIR9/ParentNew/Child1 : /DIR8/Docs/ParentOld/Child1
2 : /DIR9/ParentNew/Child2 : /DIR4/Docs/ParentOld/Child2

is -

omparing entries 1: /DIR9/ParNew/Child1 /DIR8/Docs/ParOld/Child1
: No such file or directoryOld/Child1
omparing entries 2: /DIR9/ParNew/Child2 /DIR4/Docs/ParOld/Child2
: No such file or directoryOld/Child2

Note that 21 of the lead characters of the second directory path disappear in the actual process. An of course no needed output has resulted. In addition, I also somehow manage to lose the leading letter or number in each outputted row so that the C of Comparing does not show.

Any ideas as to where I've gone wrong?

Tinkster 10-14-2009 03:55 PM

Hi,

welcome to LQ!

Did you see this thread ?


Cheers,
Tink

chrism01 10-14-2009 06:05 PM

Id that really the actual code & input & output, because they don't seem to match up:
Code:

1 : /DIR9/ParentNew/Child1 : /DIR8/Docs/ParentOld/Child1
has 3 ':', yet none of them appear in the output, in spite of the fact you split on tabs.

Are the tabs at the start of each dirpath? It would be much better if you showed the input/code/output inside LQ code tags..
The missing C is probably because you have an invisible ctrl char (a 'del') at the start there. Just manually delete the 1st 3 chars of "Comparing" and re-insert carefully.

Also, the word 'and' in your code
Code:

print "Comparing entries $DataString[0]: $DataString[1] and $DataString[2] \n";
is entirely absent from the output.

Show us the real stuff.

Also, always start all Perl progs with warnings and strict modes on:

#!/usr/bin/perl -w
use strict;

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

Phred_42 10-15-2009 09:56 AM

Sorry for the :'s

Some actual input -
1 /images10/CN/cn000161-03c /images02/Docs/AR0003S3/cn000161-03c
2 /images10/CN/cn000161-04c /images02/Docs/AR0003S3/cn000161-04c

Did the delete and re-entry thing for Comparing and got back the C.

Added use strict; , had to change the first declaration -
$DiffLocation="/DIR1/Parent/"; to my $DiffLocation="/DIR1/Parent/";

Also added - my $ShowDir = "$DataString[1] and $DataString[2]";
so that - print DIFFNAME $ShowDir; works.

However, I still get the actual error messages -
Comparing entries 1: /images10/CN/cn000161-03c and /images02/Docs/AR0003S3/cn000161-03c
: No such file or directoryS3/cn000161-03c
Comparing entries 2: /images10/CN/cn000161-04c and /images02/Docs/AR0003S3/cn000161-04c
: No such file or directoryS3/cn000161-04c

chrism01 10-15-2009 07:13 PM

Can you just post the new code (in code tags :) ), it's easier than me imagining it? The devil is in the details.
Also, post the data in code tags and specify (add notes) explaining where we've got tabs or spaces.
Thx


All times are GMT -5. The time now is 02:04 AM.