Scott Taylor wrote:

> Hello all,
>
> When I populate this hash (%SrcIDs) from "SELECT id, desc, from myTable
> order by desc" it

What does "it" refer to here?  If you mean the SQL engine, which does care about
the content of order clauses, you are mistaken.  Your data set is returned in
the proper order.  Since you have already done oredring, which you wish to
preserve, you might be better off, pushing each record into an array.

If "it" above refers to the Perl hash, that is true.  Perl hashes make no
promiswes about their internal ordering.  You order the *output of* data stored
in a ahs by applying the appropriate sort to its keys.

> doesn't order by the description field "desc".  (printing
> each row  in the while loop show that the SQL is sorted)

The test does not answer the question you think it does.  Try printing each line
as you are getting it from the fetchrow function.

>
>
> while( my($id, $desc) = $sth->fetchrow ) {
>         $SrcIDs{$id} = $desc;

You say above that you are ordering by the "desc" field.  If so, you should
probably be using desc as the keys.  If more than one id has the same desc, then
you have a small problem.  That can be remedied by using an anonymous arrays of
ids as the vlues for each keys

>
> }
> $sth->finish;

while( my($id, $description) = $sth->fetchrow ) {
   $source_ids{$description} = [] unless $source_ids{$description};
   push @{$source_ids{$description}}, $id;
}

>
>
> my @Sources = keys(%SrcIDs);
>
> # Begin HTML
> print header('text/html');
> print start_html('Select Source Mill Info Page'),
>    h3('Select Source Mill'),
>    startform('GET','srcmill.cgi','','gosrcmill','_blank'),
>    "Source Mill: &nbsp",
>      popup_menu('millid', [EMAIL PROTECTED], '', \%SrcIDs),p,
> ...
>
> So, you see I want to link @Sources with the keys of %SrcIDs, but what I
> want is to sort the hash alphabetically by the values of %SrcIDs.  Please,
> how can I do that?

To start with, describe your desired results in terms of outcomes rather than
process.  Instead of putting the information into a data strucure that you then
have to twist for desired results, just describe what you want.  I am going to
assume that you meant:

"I would like to fill a list with the IDs, sorted by the description field".

Taking the structure I showed above, this is easy:
my @flat_id_list;
foreach $description (sort keys %source_ids) {
   push @flat_id_list; (sort {$a <=> $b} @{$source_ids{$description}});
}

This should provide the ordering you sought.  The IDs conatained in
@flat_id_list will now be ordered by the description field.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to