Name the keys in alphabetic order and sort them: $SQL = "select userid a, giftid b ...."; . . . print F "$row_ref->{$_} " for sort keys %$row_ref;