$ cat Fringe.pl #!/usr/bin/perl use strict; my $tree1 = [ 'd', [ 'c', [ 'a', 'b', ], ], ]; my $tree2 = [ [ 'd', 'c' ], [ 'a', 'b' ] ]; #my $tree2 = [ [ [ 'd', 'c', ], 'a', ], 'b', ]; my $ti1 = get_tree_iterator($tree1); my $ti2 = get_tree_iterator($tree2); my $cnt_mismatches=0; while (1) { my ($L, $R) = ($ti1->(), $ti2->()); ++$cnt_mismatches unless ($R eq $L) or (!$L and !$R); print "L=$L, R=$R\n"; last if !defined $L; } print $cnt_mismatches ? "$cnt_mismatches mismatches" : "TREES MATCH!", "\n"; sub get_tree_iterator { my @stack = (shift); return sub { unshift @stack, @{shift @stack} while @stack and ref $stack[0] eq 'ARRAY'; return shift @stack; } } $ perl Fringe.pl L=d, R=d L=c, R=c L=a, R=a L=b, R=b L=, R= TREES MATCH!