Second, i personally would prefer selectall_hashref .. simplifies the database retrieval, and makes the sorting clearer and trivial .. (also just a hash works instead of a "switch")my $sql = "SELECT bug_id, owner, subject, status, assigned FROM BUGS O +RDER BY $field";
my $bugTable = $dbh->selectall_hashref("SELECT bug_id, owner, subject, + status, assigned FROM BUGS"); my %numSorts = ( 1=>'bug_id' ); my %strSorts = ( 2=>'owner', 3=>'subject', 4=>'status', 5=>'assigned' +); my $input = <STDIN>; chomp $input; my @srtArr = keys %$bugTable; if( exists $strSorts{$input} ){ my $k = $strSorts{$input}; @srtArr = sort { $bugTable->{$a}->{$k} cmp $bugTable->{$b}->{$k} } @ +srtArr; }elsif( exists $numSorts{$input} ){ my $k = $numSorts{$input}; @srtArr = sort { $bugTable->{$a}->{$k} <=> $bugTable->{$b}->{$k} } @ +srtArr; } print " @{$bugTable->{$_}} \n" for @strArr;
In reply to Re: sorting a database table with primary keys
by davidrw
in thread sorting a database table with primary keys
by pengwn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |