while (my $polygon1 = shift @polygon) { foreach my $polygons2(@polygon){ # Comparison code here } }