annel has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
let's say if
I try to delete the keys which are common in both text (only match once) and print out the mismatch. Apple of line 1 from text 1 and apple of line 1 from text 2 match, therefore deleted. But somehow apples of both lines in text 2 were deleted. Is there any better ways to delete just once for each existed data? After all of your generous help and tips,here is the code I have written. Cheers.text1: apple orange text2: apple apple
Output:use strict; use warnings; open (FILE, "a") or die "Unable to open ref file.\n"; chomp (my @data1 =<FILE>) ; close(FILE); open (FILE, "b") or die "Unable to open new file.\n"; chomp (my @data2 =<FILE> ); close(FILE); my $size = @data1 < @data2 ? @data1 : @data2; my $result= @data1 < @data2 ? "File 1 has missing data" : "File 2 has +missing data"; my $ori = @data1 == @data2 ? 1:0; my $i; for( $i = 0; $i < $size; $i++){ if ($data1[$i] ne $data2[$i]){ printf "%s is mismatch with %s\n",$data1[$i],$data2[$i]; } else { printf "%s is match with %s \n",$data1[$i],$data2[$i]; } } print "$result\n" if (!$ori);
Another method I have tried by comparing the arrays one by one. But there is a tricky part. What if Text 1 has one line while Text 2 has two lines?apple is match with apple File 2 has missing data
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl delete function
by wind (Priest) on Dec 23, 2013 at 07:04 UTC | |
by annel (Novice) on Dec 23, 2013 at 07:10 UTC | |
by wind (Priest) on Dec 23, 2013 at 07:36 UTC | |
by annel (Novice) on Dec 23, 2013 at 07:45 UTC | |
Re: Perl delete function
by taint (Chaplain) on Dec 23, 2013 at 06:30 UTC | |
by annel (Novice) on Dec 23, 2013 at 06:39 UTC | |
Re: Perl delete function
by Anonymous Monk on Dec 23, 2013 at 06:23 UTC | |
by annel (Novice) on Dec 23, 2013 at 06:39 UTC | |
by LanX (Sage) on Dec 23, 2013 at 06:48 UTC | |
by annel (Novice) on Dec 23, 2013 at 07:03 UTC | |
by hdb (Monsignor) on Dec 23, 2013 at 07:19 UTC | |
|
Back to
Seekers of Perl Wisdom