#!/usr/bin/perl -w use strict; use DBI; $|++; my $user = "username"; my $pass = "password"; my $dbnm = "database"; my $table = "table"; my $dbh = DBI->connect("DBI:mysql:$dbnm", $user, $pass); my $ret = $dbh->selectcol_arrayref("select line_n, word from $table", { Columns => [ 1, 2 ] }); $dbh->disconnect; my %seen; my @list = @$ret; while (@list){ my $line_n = shift @list; my $word = shift @list; print $seen{$line_n}++ ? " " : "\n$line_n: "; print $word; }