in reply to Using Array::Diff

I know this is not using Array::Diff, but it just /might/ be a different way to do this:
use strict; use warnings; my @old = qw(one two three); my @new = qw(one foo two bar three baz); print '@old array' . "\n"; print "$_\n" for (@old); print "\n" . '@new array' . "\n"; print "$_\n" for (@new), "\n"; foreach my $element (@new) { if ( $element ~~ @old ) { print "matched $element"; } else { print "\ndidnt match $element...Adding $element to old array\n +"; push( @old, $element ); } } print "\n" . '@old array' . "\n"; print "$_\n" for (@old);
But i am unsure if this is what you were looking for but if anything in the new array is not found in the old array it is added to the old array and will be skipped if found again. A counter could be easily added as well on each non match.

Replies are listed 'Best First'.
Re^2: Using Array::Diff
by Laurent_R (Canon) on Dec 03, 2014 at 07:48 UTC
    This does only one part of the job, because works only one way: it detects elements in @new_array and not in @old_array, but not elements in @old_array which are not in @new_array (i.e. deleted elements). Arrat::Diff does the check in both ways.

    Also, if the lists are long, the performance is not very good because you have essentially two loops (the smat match over an array being an implicit loop), so the complexity is higher than using hashes (which Array::Diff does, if I remember correctly).

      Yes, I am learning/migrating some of my own script to use hashes as well. They are indeed much faster than arrays in the few tests I tried. Sometimes, (or more like all the time lol) I wish I could go through the perldocs and everything just sink in clear and understandable the first time around.