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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.