my @arrays = ( [] ); my $length = 0; while (<>) { if (length $_ + $length >= 24 * 1024) { push @arrays,[$_]; $length = length $_; } else { push @{$arrays[-1]},$_; $length += length $_; } }