OK, BrowserUK, here we go with a sameFringe function and the test data copied from your posted solution.

Note that I had to make a small change in my closure (unshift instead of push on the @ref_list array), because my original code did not compare $a and $c correctly (the leaves were appearing in opposite order).

#!/usr/bin/perl use strict; use warnings; my $a = [ 1, [ 2, [ 3, [ 4, 5 ] ] ] ]; my $b = [ 1, [ [ 2, 3 ], [ 4, 5 ] ] ]; my $c = [ [ [ [ 1, 2 ], 3 ], 4 ], 5 ]; sameFringe( $a, $a ); sameFringe( $a, $b ); sameFringe( $a, $c ); my $x = [ 1, [ 2, [ 3, [ 4, [ 5, 6 ] ] ] ] ]; my $y = [ 0, [ [ 2, 3 ], [ 4, 5 ] ] ]; my $z = [ 1, [ 2, [ [ 4, 3 ], 5 ] ] ]; sameFringe( $a, $x ); sameFringe( $a, $y ); sameFringe( $a, $z ); sub sameFringe { my $next_el1 = create_iterator(shift); my $next_el2 = create_iterator(shift); my $match = 1; while (1) { my $left = $next_el1->(); my $right = $next_el2->(); no warnings 'uninitialized'; print $left, " ", $right, "\n"; unless ($left eq $right) {$match = 0 ; last} ; last unless defined $left; } $match ? print "the trees match\n": print "the trees don't match\n +"; } sub create_iterator { my $ref = shift; my @ref_list; return sub { while (ref $ref eq 'ARRAY') { unshift @ref_list, @$ref; $ref = shift @ref_list; } my $leaf = $ref; $ref = shift @ref_list; return $leaf; } }

The following is the output:

>perl fringe.pl 1 1 2 2 3 3 4 4 5 5 the trees match 1 1 2 2 3 3 4 4 5 5 the trees match 1 1 2 2 3 3 4 4 5 5 the trees match 1 1 2 2 3 3 4 4 5 5 6 the trees don't match 1 0 the trees don't match 1 1 2 2 3 4 the trees don't match

In reply to Re^2: Challenge: Perl 5: lazy sameFringe()? by Laurent_R
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.