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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |