Whee! A chance to learn something new :) It's the first time I used "key generation functions" in Algorithm::Diff.
#!/usr/bin/perl # http://perlmonks.org/?node_id=1195036 use strict; use warnings; use Algorithm::Diff qw(traverse_sequences); my @array1=('adam west', 'daric dalon','tom helic','todd nick', 'riley remer'); my @array2=('adam west 12', 'daric dalon mr.','tom helic (fads)', 'todd nick (456)','riley remer','john steve','dim madz 12'); # generate a hash for the key generation function $_ = join '', map "$_\n", sort @array1, @array2; my %keyhash; $keyhash{$1} = $keyhash{$2} = $1 while /^(.*)\n(?=(\1.*)\n)/gm; # match traverse_sequences( \@array1, \@array2, { MATCH => sub {print " matched $array1[shift()] -- $array2[pop() +]\n"}, DISCARD_A => sub {print "unmatched $array1[shift()]\n"}, DISCARD_B => sub {print "unmatched $array2[pop()]\n"}, }, sub { $keyhash{$_[0]} // $_[0] }, );
This prints:
matched adam west -- adam west 12 matched daric dalon -- daric dalon mr. matched tom helic -- tom helic (fads) matched todd nick -- todd nick (456) matched riley remer -- riley remer unmatched john steve unmatched dim madz 12
Is this what you were looking for?
In reply to Re: Matching arrays with different number of elements and element size
by tybalt89
in thread Matching arrays with different number of elements and element size
by Ksonar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |