my $modified = reduce{ my( $x1, $y1, $x2, $y2 ) = ( @{ @{ $a }[ -1 ] }, @$b ); $x2 < $x1 ? pop @{ $a } : push @{ $a }, $b; $a; } [ shift @points ], @points;