Sort Results Numerically - brief 
Author Message
 Sort Results Numerically - brief


> Could someone help me sort these results numerically?

> I have found a sort routine:

> The problem is, I wish to sort by a calculated value $distance, which is
> done for each record.
> I want to sort my results numerically according to the $distance for each
> result.

> record),
> but NOT the actual Fields of the records in the datafile.

> Can anyone help?

[ some code deleted ]


>            ##### possible location for numerical sorting

>            {&display}
> }

Errhm, you want to do the numerical sorting _before_ the loop, like so:

$field[3] }


This can be a bit slow, since you'd be splitting records quite a lot.

It's more efficient to just compute the distance once and remember it.
This trick is known as the "Schwartzian Transform", after Randal L. Schwartz.

$field[3]; }

In fact, it's even more efficient to split each record just once, when
it's read in,
and work with split records from then on:

        open(DATA, "$Folder/data.txt") or die "Can't read $Folder/data.txt: $!\n";
        while (<DATA>) {

                my $distance = $field[4] - $field[3];

                ## rejects any record with value $distance over 50

        close DATA;

        print "$Header1\n";

                print " No matches\n";
        } else {

                        print " Company: $ad->[6], City: $ad->[2], ",
                                        "State: $ad->[3], Distance: $ad->[0]<HR>\n";

        print "$Footer1\n";

Hope this helps,

-- HansM

Fri, 29 Aug 2003 09:55:15 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Paradox 'index out of date' message

2. Would LOVE help sorting results numerically - clearing up an array issue

3. sort numerically descending is not right?

4. sorting strings numerically

5. sorting strings numerically

6. problem with sort numerically

7. sort 2d array numerically by row?

8. How does one sort associative array numerically?

9. Sorting Numerically

10. Sorting Numerically

11. Sorting Keys numerically

12. Sorting a file numerically with Perl


Powered by phpBB® Forum Software