in reply to Order a hash?

I'm wondering ... unless you do something with the hash after the foreach loop (not shown), why do you read everything into it in the first place?

You could iterate over the single lines of what the database returns (at least using DBI 1.12, don't know about pre-1.0-versions), therefore preserving the order generated by the database (your "order by" statement):

... $dbh = DBI->connect("DBI:mysql:$database:$hostname", $user, $password) +; $statement = "select ... order by ..."; $sth = $dbh->prepare($statement) or die "Can't prepare $statement: $dbh->errstr\n"; $rv = $sth->execute or die "can't execute the query: $sth->errstr\n"; my @row; while(@row = $sth->fetchrow_array) { # @row contains the individual columns # ... do work ... } $rc = $sth->finish; $rc = $dbh->disconnect;
8-)

Andreas