my @lines; while (length $_) { my $p=substr($=, 0, 1, ''); while ($p ne ' ') { print "Remaining text: $_\n"; print "p='$p'"; die "line to short, but no previous line?" unless length $lines[-1]; print "Previous line: $lines[-1]\n"; $_=substr($lines[-1], -1, 1, ''); $p=substr($_, 0, 1, ''); } }