in reply to Re: Question on file compare
in thread Question on file compare

Thanks a million to all for helpful suggestions. Dear Marshall, I made all changes as suggested. I am not getting any errors But looks like the "if ($File2Parts{$PartNumber})" is not working as expected. If I put the "print F3 ..." line outside the if condition it is writing File3. Any ideas? Here is my modified code:

#!/usr/bin/perl -w use strict; my $FILE1 = "D:/Data/WES/File1.txt"; # You can use "/" my $FILE2 = "D:/Data/WES/File2.txt"; # avoids this \\ stuff my $FILE3 = "D:/Data/WES/File3.txt"; open(F1,'<', $FILE1) or die "Can't open $FILE1\n"; open(F2,'<', $FILE2) or die "Can't open $FILE2\n"; open(F3,'>', $FILE3) or die "Can't open $FILE3\n"; #F3 header goes here my %File2Parts; # why call it F1parts? these numbers are # coming from file 2. names matter. while (my $PartNumber = <F2>) { chomp $PartNumber; next if $PartNumber =~ /^\s*$/; # skip blank lines # often a "unseen" trailing blank line # can cause troubles $File2Parts{$PartNumber} = 1; } close F2; while (my $uline = <F1>) { chomp $uline; #you need this when splitting on other than white s +pace my @ufields = split(/\|/, $uline); my ($PartNumber, $Std_Cost, $Last_Paid_Price, $Qty_In_Stock, $Moto_Preferred_Part, $Rev, $Agile_Description) = (@ufields)[0,1,2,3,7,9,10]; # print F3 "$PartNumber|$Std_Cost|$Last_Paid_Price|$Qty_In_Stock|$ +Moto_Preferred_Part|$Rev|$Agile_Description\n"; if ($File2Parts{$PartNumber}) { print F3 "$PartNumber|$Std_Cost|$Last_Paid_Price|$Qty_In_Stock +|$Moto_Preferred_Part|$Rev|$Agile_Description\n"; } } close F1; close F3;

Replies are listed 'Best First'.
Re^3: Question on file compare
by Marshall (Canon) on Mar 14, 2012 at 00:25 UTC
    Well something is wrong with if ($File2Parts{$PartNumber}) That statement checks for "truthfullness" of the value of that hash key's value.

    Add: use Data::Dumper; at the top of the code. Then before the F1 loop, print Dumper \%$File2Parts; That will show you what is actually in the hash table. Data::Dumper is a "core module" meaning that it is already "pre-installed" in Perl. Look for spaces or other characters that would cause the hash key from file2 to not compare with the PartNumber from file1.

    Of course use some abbreviated files for testing otherwise you will get a lot of output that is not helpful!

      Thanks Marshall. I finally got it to work. There were blank spaces in $PartNumber. Adding a $PartNumber =~ s/^\s+|\s+$//g; in the F1 loop fixed the issue. Thanks again to you and all those who replied. I will never forget this help.