Hi all,
let's say if
text1:
apple
orange
text2:
apple
apple
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.
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);
Output:
apple is match with apple
File 2 has missing data
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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|