Operating System - Linux
1751851 Members
5333 Online
108782 Solutions
New Discussion юеВ

Re: Change diff-Algorithmus

 
SOLVED
Go to solution
Kalin Evtimov
Regular Advisor

Change diff-Algorithmus

Hallo!
I am using this algorithm to compare two files.

Code:

@one = qw(a a a a b c d e f g);
@two = qw(b c e h i i i i j);
my %tracker = ();
$tracker{$_} .= 1 for @one;
$tracker{$_} .= 2 for @two;
for (sort keys %tracker) {
if ($tracker{$_} !~ /1/) {
print "$_ has been added\n";
} elsif ($tracker{$_} !~ /2/) {
print "$_ has been deleted\n";
} else {
print "$_ is in both old and new\n";
}
}


It has following output:


a has been deleted
b is in both old and new
c is in both old and new
d has been deleted
e is in both old and new
f has been deleted
g has been deleted
h has been added
i has been added
j has been added


here "a" is mentioned only once, since there are 4 "a"-s in the first array. What can I chnage in order to get them all appearing?

Thank you!
6 REPLIES 6
Peter Godron
Honored Contributor

Re: Change diff-Algorithmus

Hi,
not really a solution, but have you had a look at the "comm" command ?
Kalin Evtimov
Regular Advisor

Re: Change diff-Algorithmus

No :(
Kalin Evtimov
Regular Advisor

Re: Change diff-Algorithmus

comm will not wirk, because I am comparing log files and if I sort them, I won't be able to extract the last changes that happened.
Peter Godron
Honored Contributor
Solution

Re: Change diff-Algorithmus

Ok,
how about:
@one = qw(a a a a b c d e f g);
@two = qw(b c e h i i i i j);
my %tracker = ();
$tracker{$_} .= 1 for @one;
$tracker{$_} .= 2 for @two;
for (sort keys %tracker) {
$tester = length($tracker{$_});
if ($tracker{$_} !~ /1/) {
print "$_ has been added $tester time(s)\n";
} elsif ($tracker{$_} !~ /2/) {
print "$_ has been deleted $tester time(s)\n";
} else {
print "$_ is in both old and new\n";
}
}
Peter Godron
Honored Contributor

Re: Change diff-Algorithmus

Kalin,
how are you getting on with this?
Can you pleas update.
Kalin Evtimov
Regular Advisor

Re: Change diff-Algorithmus

Yeah, of course.
I was just playing around with this piece of code.
You idea worked for me perfectly. I just put the $tester in a for-loop to print $tester-times the element and now I have it.

Thank you for this idea!