I'm probably missing something but ... fringe of a list (containing string/numbers and lists containing strins/numbers and lists containing ...) are the strings/numbers in a totally flattened list, right? So the samefringe() function should check that the list contains the same strings/numbers in the same order disregarding the structure, right? Without creating the totally flattened lists, right? (Sorry I got lost in the maze so I have no idea whether you do or don't create some lists like that or modify the data structure along the way.)

use strict; use warnings; sub samefringe { my ($x, $y) = @_; my ($ix, $iy) = (0,0); samefringe_r($x, $y, $ix, $iy) and $#{$x}+1 == $ix and $#{$y}+1 == + $iy; } sub samefringe_r { my ($x, $y, $ix, $iy) = @_; while ($ix <= $#{$x} and $iy <= $#{$y}) { if (ref($x->[$ix])) { my $i_ix = 0; samefringe_r( $x->[$ix], $y, $i_ix, $iy) or return; $ix++; } elsif (ref($y->[$iy])) { my $i_iy = 0; samefringe_r( $x, $y->[$ix], $ix, $i_iy) or return $iy++; } elsif ($x->[$ix] ne $y->[$iy]) { return } else { $ix++; $iy++; } } $_[2]=$ix;$_[3]=$iy; return 1; } my $l1 = [ "a", [qw(b c)], "d" ]; my $l2 = [ qw(a b c d) ]; my $l3 = [ qw(a b c d e)]; my $l4 = [ qw(a x c d)]; print "1 x 2, should be same : ".(samefringe($l1, $l2) ? 'same' : 'dif +f')."\n"; print "1 x 3, should be diff : ".(samefringe($l1, $l3) ? 'same' : 'dif +f')."\n"; print "1 x 4, should be diff : ".(samefringe($l1, $l4) ? 'same' : 'dif +f')."\n"; print "2 x 3, should be diff : ".(samefringe($l2, $l3) ? 'same' : 'dif +f')."\n"; print "2 x 4, should be diff : ".(samefringe($l2, $l4) ? 'same' : 'dif +f')."\n";

In reply to Re: No Iterators Allowed by Jenda
in thread No Iterators Allowed by runrig

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.