hesco has asked for the wisdom of the Perl Monks concerning the following question:
I understand that hashes are unordered. But surely someone has devised a simple and elegant way to address this issue before I grappled with it. Any ideas out there?
-- Hugh
UPDATE:my $j = 0; my($line,$field,$record); RECORD: while ($record = $sth->fetchrow_hashref){ $line = ''; if($j == 0){ foreach $field (keys %$record){ $line .= $field . "\t"; } print RESULTS $line,"\n"; $j++; next RECORD; } $j++; foreach $field (keys %$record) { if (!defined($record->{$field})) { $record->{field} = ''; } else { $record->{$field} =~ s/\t/ /g; $record->{$field} =~ s/\r//g; if (/\n/) { $record->{$field} = s/(\s)\n/$1/g; $record->{$field} = s/\n(\s)/$1/g; $record->{$field} = s/\n$//; $record->{$field} = s/\n/ /g; } } $line .= $record->{$field} . "\t"; } print RESULTS $line,"\n"; } close(RESULTS);
bobf:
You're the man!
That is exactly what I was looking for. And here is the patch which did the trick. Of course I had to apply it in two places, to the loop which created the header of field names, as well as to the loop which constructed each record in the report.
Now everything appears in the order defined by the query, even if I just grab a SELECT * FROM table; and the order is given by the organization of the table queried. Thanks again.- foreach $field (keys %$record){ + foreach $field (@{$sth->{NAME}}) {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Controlling order of fields in report
by samtregar (Abbot) on Oct 15, 2007 at 17:56 UTC | |
|
Re: Controlling order of fields in report
by NetWallah (Canon) on Oct 15, 2007 at 18:01 UTC | |
|
Re: Controlling order of fields in report
by bobf (Monsignor) on Oct 16, 2007 at 03:01 UTC |