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