in reply to fetching data from MySQL

I'd probably do what others have suggested, fetch the rows separately and combine them in Perl. But you might be interested to know that MySQL can do this all for you:

SELECT CONCAT("Line ", line_n, " ", GROUP_CONCAT(word ORDER BY word_position SEPARATOR " ")) FROM words GROUP BY line_n ORDER BY line_n;

GROUP_CONCAT is a powerful tool indeed - very useful in report building.

-sam