use warnings; use strict; my @lines; while (<>) { push @lines, $_; if ($. % 3 == 0) { # do something with @lines; @lines = (); } }