#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my $i = 1; my $sixindex = 0; my @copy = @$aref; if (scalar(@$aref) >= 2) { foreach my $x (0..$#{$aref}) { if (defined($aref->[$x])) { if ($aref->[$x] == 6) { $flag = 1; $sixindex = $x; next; } if ($flag) { unless ($aref->[$x] == 7) { $i++; next; } elsif ($aref->[$x] == 7 ) { splice(@copy, $sixindex, $i + 1); $flag = 0; $i = 1; $sixindex = 0; next; } } } } } print Dumper \@copy; #### ./ignore_6_to_7.pl $VAR1 = [ 1, 1, 6, 99, 99, 7 ]; #### Result Expected [1, 1] by deleting all the numbers in the list starting with 6 and until it finds the first available 7 .