while (){ chomp; if (/^Number/) { # found header, now we're expecting two lines with numbers for (1..2) { $_ = ; chomp; my @numbers = split; #... } } #... do other stuff }