OK then, the outer arrays are essentially the wrong structure for doing a comparison - you'd be better off with hashes of hashes. But perhaps the outer arrays make sense in other parts of your code, so let's stick with them and just convert to hash temporarily while doing the comparison.

The following example copies both sets of data into a temporary hash to make the comparisons a bit easier. It should run in roughly O(n) time. In other words, the amount of time it takes to run should increase linearly (rather than quadratically or exponentially) with the number of items being processed. Of course, building the temporary hash structure uses up extra memory. You're trading memory for CPU time.

# Some example data... my @curr = ( { node => 'Alice', link => 'A to B', load => 20, }, { node => 'Alice', link => 'A to C', load => 24, }, { node => 'Bob', link => 'B to C', load => 12, }, { node => 'Bob', link => 'B to A', load => 3, }, { node => 'Carol', link => 'C to A', load => 14, }, { node => 'Carol', link => 'C to B', load => 7, }, ); my @prev = ( { node => 'Alice', link => 'A to B', load => 20, }, { node => 'Alice', link => 'A to C', load => 24, }, { node => 'Bob', link => 'B to C', load => 15, }, { node => 'Bob', link => 'B to A', load => 3, }, { node => 'Carol', link => 'C to A', load => 12, }, { node => 'Carol', link => 'C to B', load => 3, }, ); # Transform to a hash. This happens in roughly O(n) time. my %hash; $hash{"$_->{node}|$_->{link}"} = +{%$_} for @curr; $hash{"$_->{node}|$_->{link}"}{prev_load} = $_->{load} for @prev; # Again, looping through the hash values is roughly O(n). for (values %hash) { printf "Node '%s', link '%s': previously %d, currently %d.\n", $_->{node}, $_->{link}, $_->{prev_load}, $_->{load}, unless $_->{load} == $_->{prev_load}; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^3: Comparing elements in Array of Hashes (AoH) by tobyink
in thread Comparing elements in Array of Hashes (AoH) by hmadhi

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.