TIMTOWTDI

NOTE that this example iterates over HoH not AoAs (which is a bit silly b/c order matters) since I have only 5.10 installed.

With >=5.12 each should also work with ARRAYs! (testing appreciated)

It might not be the most elegant solution but

a) it's really lazy - not only for the programmer - and

b) it can iterate nested Arrays and Hashes alike for >=5.12.

c) it's quite fast.

d) I can tell the recursion "depth" of each iteration. NOTE I'm only returning 3 values for better visualization/debugging. Only $v matters for this task.

use Data::Dump 'dd'; use strict; use warnings; dd my $h1 = { a => {b => {c => 1}, d=> 2}, e=>3,f=>4 }; dd my $h2 = { a => {b => {c => 1}, d=> 2}, e=>3,f=>4 }; sub gen { my $ref=shift; my @path=(); return sub { while (1) { while (my ($k,$v) = each %$ref) { if ( ref $v eq "HASH") { push @path,$ref; $ref =$v; next; } else { return $k,$v,scalar @path; } } return unless $ref = pop @path; } } } my $iter1=gen($h1); my $iter2=gen($h2); while ((my ($k1,$v1,$l1) = $iter1->()) + (my ($k2,$v2,$l2) = $iter2->( +)) ) { die "error $v1 != $v2" if $v1 ne $v2; print "$l1: $k1=> $v1\n"; }

out

/usr/bin/perl -w /tmp/lanx_fringe.pl { a => { b => { c => 1 }, d => 2 }, e => 3, f => 4 } { a => { b => { c => 1 }, d => 2 }, e => 3, f => 4 } 0: e=> 3 2: c=> 1 1: d=> 2 0: f=> 4
a more functional solution on Monday.

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

I just realized that it's about binary trees and that the representation of them is free.

So when choosing nested hashes with keys L and R , this algorithm already delivers the needed features. =)

UPDATE

Limitations see Re^3: Challenge: Perl 5: lazy sameFringe()?


In reply to Re: Challenge: Perl 5: lazy sameFringe()? by LanX
in thread Challenge: Perl 5: lazy sameFringe()? by BrowserUk

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.