my @dates; foreach $odate (@logs) { if ($odate =~ /\bdate=(\d{1,4}-\d{1,2}-\d{1,2})/i) { push @dates, $1; print "Found $1 as a new date\n"; } } print "I collected the following dates: @dates\n"; for my $date (@dates) { print "Processing $date\n"; }; #### while (@logs = ) { foreach (@logs) { ... #### while (my $odate = ) { ... };