MySQL Lists are EOL. Please join:

List:General Discussion« Previous MessageNext Message »
From:Urb LeJeune Date:August 18 1999 2:54pm
Subject:Re: $sth->fetchrow_hashref
View as plain text  
	As previously noted by several others, a hash can be sorted 
by key value using:

foreach $key (sort keys %somehash) {
   print "$key = $somehash{$key}";
}

several people have submitted methods of sorting the values of a has
as saving the results in an array. Most typically in DB applications
we want the values sorted, not the keys. The values of a hash can be 
sorted directly using:


foreach $key (sort ByValue keys %somehash) {
    print "$key = $somehash{$key}";
}
sub ByValue {
    $somehash{$a}  cmp $somehash{$b};
}

cmp evaluates string returning 1 if the left side is greater, -1 if it's
less and 0 if
both sides are equal. <=> operates on numbers, returning the same values.

 "9" cmp "12" returns 1 since in a string context 9 is greater than 12.
 9  <=> 12  returns -1 since 12 is greater than 9. 

	To return to the original post, since no sorting technique will return
a hash's values by table order (except by chance), there are two solutions
to:

>while ( my $rowref = $sth->fetchrow_hashref ) {
>	foreach $db_field (keys %{$rowref} ) {
>  		print 
>"<TR>\n<TD>$db_field:</TD><TD>$rowref->{$db_field}</TD>\n</TR>\n";

	Since the column are presumably know, let's assume field_1, fiels_2, etc,
the above could be changed to:

@Names  = qw/field_1 field_2/;

$sub=0.
while ( my $rowref = $sth->fetchrow_hashref ) {
	foreach $db_field (keys %{$rowref} ) {  
             my $Name = $Names[$Sub];
 		print 
 "<TR>\n<TD>$Name</TD><TD>$rowref->{$Name}</TD>\n</TR>\n";

	In actuality, this is not a good way of performing the stated task.
Array processing is substantially more efficient than hash processing. 
Since (presumably) the field names are know, use an array.

  my $Row = $sth->fetchrow_arrayref;

the value of the first field is contained in    = $$Row[0]
the second in          = $$Row[1]

and so on. Load a name array such as:

@Names  = qw/field_1  field_2/;

then:

my $Row = $sth->fetchrow_arrayref;
for($Sub=0;$Sub<=$LastIndex;$Sub++) {
  print "$Names[$Sub] contains $$Row[$Sub]\n";
}

	Not that $Row is a reference to an array so accessing an
element required defererencing with $$Row.

	BTW, a nicer way to write
  		print 
"<TR>\n<TD>$db_field:</TD><TD>$rowref->{$db_field}</TD>\n</TR>\n";

is with Perl's "here document" construct:

print<<ROW;
<TR>
  <TD>$db_field:</TD>
  <TD>$rowref->{$db_field}</TD>
</TR>
ROW

	I like to accumulate on the fly and print once: Something like:

loop construct
$DislayString.=<<Display;
 a row as above
Display
end loop
print $DisplaySting;

one I/O instruction insted of (rows * columns) I/O instructions.

Urb LeJeune

Thread
$sth->fetchrow_hashreftoxalot18 Aug
  • Re: $sth->fetchrow_hashrefPaul DuBois18 Aug
  • Re: $sth->fetchrow_hashrefJeff Urban18 Aug
    • Re: $sth->fetchrow_hashrefPaul DuBois18 Aug
    • Re: $sth->fetchrow_hashrefSebastian Hoffmann18 Aug
      • Re: $sth->fetchrow_hashrefLuuk de Boer18 Aug
        • Re: $sth->fetchrow_hashrefUrb LeJeune18 Aug