#!/usr/bin/perl use warnings; use strict; use Data::Dump; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my @copy; if (scalar(@$aref) >= 2) { foreach my $x (@$aref) { dd $aref; dd \@copy; if (defined($x)) { if ($x == 6) { $flag = 1; next; } if ($flag) { next unless $x == 7; $flag = 0; next; } print "push \@copy,$x\n"; push @copy, $x; } } } dd \@copy;