Sort Problem 
Author Message
 Sort Problem

I need help sorting a result set. Here is the code snippet that causes
problems:

# early in the routine:

                    'CompanyName','Title', 'Address1', 'Address2',
                    'City', 'StateOrProvince', 'PostalCode', 'WorkPhone',
                    'MobilePhone', 'FaxNumber');

# later, $results holds a hashref of hashrefs such that :
# $results= { '1234'=>{ContactID=>'1', FirstName=>'Bob', etc. }

# then I call print_results, as:


sub print_results {

    print header(),
            start_html(    -title=>'Database Search',
                        -style=>{-src=>'/clients2.css'},
                        -script=>{-src=>'/clients.js'} );

    my $order = param('sort') || 2;

    # Line that causes problems.
    foreach my $ContactID (sort{ $results->{$a}{$fields[$order]}
                                    cmp
                                 $results->{$b}{$fields[$order]}
                                    ||
                                 $results->{$a} <=> $results->{$b}
                                } keys %$results)
    {

        # Do some stuff with $results->{$ContactID}
    }

Quote:
}

The problem is sometimes $results->{$a}{$fields[$order]} is not defined, and
it throws an error message under warnings and strictures. Is there a better
way to solve this?

Thanks,
cp



Mon, 27 Dec 2004 21:13:51 GMT  
 Sort Problem

on 7/11/02 3:13 PM:

Quote:
> I need help sorting a result set. Here is the code snippet that causes
> problems:

> # early in the routine:

>                   'CompanyName','Title', 'Address1', 'Address2',
>                   'City', 'StateOrProvince', 'PostalCode', 'WorkPhone',
>                   'MobilePhone', 'FaxNumber');

{sigh}

In my previous post, I cut and pasted the subroutine, but I retyped the
intro for brevity. Obviously, that array should be:


                    'CompanyName','Title', 'Address1', 'Address2',
                    'City', 'StateOrProvince', 'PostalCode', 'WorkPhone',
                    'MobilePhone', 'FaxNumber' );

Sorry about that.
cp



Mon, 27 Dec 2004 21:19:53 GMT  
 Sort Problem

Quote:

> I need help sorting a result set. Here is the code snippet that causes
> problems:

> # early in the routine:



Quote:
> # later, $results holds a hashref of hashrefs such that :
> # $results= { '1234'=>{ContactID=>'1', FirstName=>'Bob', etc. }
>     # Line that causes problems.
>     foreach my $ContactID (sort{ $results->{$a}{$fields[$order]}
>                                     cmp
>                                  $results->{$b}{$fields[$order]}
>                                     ||
>                                  $results->{$a} <=> $results->{$b}
>                                 } keys %$results)
> The problem is sometimes $results->{$a}{$fields[$order]} is not defined, and
> it throws an error message

No it doesn't.

You must have meant "warning message" instead.

Quote:
> under warnings

warnings never generate error messages.

warnings are not fatal.

Quote:
> and strictures.

strictures does not care about values (undef), it only cares
about variables. It has nothing to do with whatever mystery
message you are referring to.

Quote:
> Is there a better
> way to solve this?

You do not say what you want to do when the sort field does not exist.

I'll assume you want to sort by ContactID in that case.

    foreach my $ContactID (sort{
          ($results->{$a}{$fields[$order]} or $results->{$a}{ContactID})
                                    cmp
          ($results->{$b}{$fields[$order]} or $results->{$b}{ContactID})
                                    ||
          $results->{$a} <=> $results->{$b}
                                } keys %$results)

I'd convert the above to some more efficient method (perldoc -q sort)
if the hash might become "large".

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Mon, 27 Dec 2004 23:42:07 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sort of a sort problem.

2. Sorting problem

3. list sorting problem, please help

4. Sorting problem...

5. FormMail sort problem w/realname,email

6. sorting problem

7. Simple? sort problem

8. Funky sorting problem!!

9. sorting problem

10. Sorting problem -New to Perl

11. sorting problem

12. sort problem

 

 
Powered by phpBB® Forum Software