in reply to fetching data from MySQL

Something like this could do the work:

#connect to the database and issue the following query; #select * from tablename order by line_n,word_position asc #then loop through the results and print the lines my (@row, $row); my $line_no = -1; my $line = ''; while(@row = $sth->fetchrow_array) { if($line_no == $row[1]) { $line .= " $row[2]"; } else { print "Line $line_no $line"; unless ($line_no == -1); $line = ''; $line_no = $row[1]; } } # note that this is untested code.