my @buffer = (); my $length = scalar @sourcedata; for (my $I = 0; $I < $length; $I++) { my $line = $sourcedata[$I]; if ($line =~ /^1$/) { push(@buffer, $line); $I++; $line = $sourcedata[$I]; # Here's the loop I can't seem to execute: while ($line =~ /^\n$/ && $I != $length) { print "Buffering...\n"; push(@buffer, $line); $I++; last if $I == $length; $line = $sourcedata[$I]; } if ($line =~ /_____ 2/) { # print only the first and last items in the buffer, # effectively removing the empty lines print shift(@buffer), pop(@buffer); print $line; } else { print join(@buffer); } } else { print $line; } @buffer = (); }