#!/usr/bin/perl
use strict;
use warnings;
my $tree1 = [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]];
my $tree2 = [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]];
my $tree3 = [1, [2, [4, [7]], [5]], [3, [6, [9], [8]]]];
my $next_el1 = create_iterator($tree1);
my $next_el2 = create_iterator($tree3);
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;
}
if ($match) {
print "The trees match \n";
} else {
print "The trees don't match \n";
}
sub create_iterator {
my $ref = shift;
my @ref_list;
return sub {
while (ref $ref eq 'ARRAY') {
push @ref_list, @$ref;
$ref = shift @ref_list;
}
my $leaf = $ref;
$ref = shift @ref_list;
return $leaf;
}
}
####
$ perl fringe.pl
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 9
the trees don't match
####
$ perl fringe.pl
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
The trees match