open (MAN, "/usr/bin/man ssh |"); while () { s/^\s+//g; push(@ml,$_); } # apply the substitutions to each element of @ml map (s/\n$//, @ml); map (s/^\n//, @ml); map (s/^\s+//, @ml); for ($x=0; $x<@ml; $x++) { if ($ml[$x] =~ /NAME/) { # new loop starting at one element past # the current, appending the current line # to $nl. Stops when SYNOPSIS is encountered for ($x++; $ml[$x] !~ /SYNOPSIS/; $x++) { $nl .= $ml[$x]; } } }