Hello jokerzy,

(I used Data::Dumper to print the hash out, and I think it's a hash of arrays of (ref to) hash, correct?)

Actually, it’s a hash of refs to anonymous arrays, the elements of which are refs to anonymous hashes — but you’ve got the idea! :-)

As to the code, frozenwithjoy had the right intuition: the logic of your inner loop is incorrect. However, the fix requires a different approach:

#! perl use strict; use warnings; use Data::Dumper; my %main_hash = ( keyA => [ { keya => 'vala' }, { keyb => 'valb' }, { keyc => 'valc' }, { keyd => 'vald' }, ], keyB => [ { keye => 'vale' }, { keyf => 'valf' }, { keyg => 'valg' }, { keyh => 'valh' }, ], keyC => [ { keyi => 'vali' }, { keyj => 'valj' }, ], ); print Dumper(%main_hash), "\n"; foreach my $k (sort keys %main_hash) { my @array = @{ $main_hash{$k} }; foreach my $i (1 .. $#array) { my %inner_hash_first = %{ $array[$i - 1] }; my %inner_hash_next = %{ $array[$i ] }; my ($first_key, $first_val) = each( %inner_hash_first ); my ($next_key, $next_val) = each( %inner_hash_next ); print "Now compare ($first_key : $first_val) with ($next_key : + $next_val)\n"; } }

Note in the above script that dereferencing has been decomposed into separate stages by using intermediate variables (@array, %inner_hash_first, and %inner_hash_next). This is a good idea for two reasons:

  1. It makes the code clearer, and so easier to follow and debug.
  2. In some cases it also promotes efficiency: for example, @{ $main_hash{$k} } is now re-calculated only once for each new value of $k.

The output from the above script is:

Now compare (keya : vala) with (keyb : valb) Now compare (keyb : valb) with (keyc : valc) Now compare (keyc : valc) with (keyd : vald) Now compare (keye : vale) with (keyf : valf) Now compare (keyf : valf) with (keyg : valg) Now compare (keyg : valg) with (keyh : valh) Now compare (keyi : vali) with (keyj : valj)

which shows that each value of each inner hash is available for comparison with its successor (if it has one), as required.

(You should probably also add a check to verify that each inner hash contains exactly one key/value pair.)

Update

Is that a pseudo-hash I'm using here?

No, according to perldoc, pseudohashes “have been removed from Perl.” See the old thread My views on pseudohashes.

HTH,

Athanasius <°(((><contra mundum


In reply to Re: Question about dereferencing multi-dimensional array/hash by Athanasius
in thread Question about dereferencing multi-dimensional array/hash by jokerzy

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.