CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sorting from a database

 



MarkehB
Deleted

Feb 6, 2001, 10:51 AM

Post #1 of 6 (508 views)
Sorting from a database Can't Post

Ok, I have a perl cgi script that is a league table, its pretty simple, comes for a flatfile db file.

What I want to do is sort it, I dont have a clue how, any ideas?

Thanks all
Mark



muinatit
Deleted

Feb 6, 2001, 12:48 PM

Post #2 of 6 (506 views)
Re: Sorting from a database [In reply to] Can't Post

The sort command. Say you have the db contents in an array...


Code
@array = sort(@array);

muinatit
muinatit@hotpop.com


MarkehB
Deleted

Feb 6, 2001, 1:37 PM

Post #3 of 6 (505 views)
Re: Sorting from a database [In reply to] Can't Post

Cheers m8 :)

Btw is there anyway to define priority for the sort, say points is the primary, then goals is the backup incase 2 teams have the same no of points?



muinatit
Deleted

Feb 6, 2001, 3:29 PM

Post #4 of 6 (499 views)
Re: Sorting from a database [In reply to] Can't Post

There's a way I can think of but it's a bit advanced. I'd also need to look up a few commands... lemme see what I can pull together.

muinatit
muinatit@hotpop.com


Knight
Deleted

Feb 7, 2001, 2:49 AM

Post #5 of 6 (491 views)
Re: Sorting from a database [In reply to] Can't Post

I have got it to sort using:

@DB = sort { $a <=> $b } @DB;

but i need to osrt by 2 diff columns, and need to define wot they are, ne ideas?

Mark



kencl
User

Mar 1, 2001, 11:42 PM

Post #6 of 6 (469 views)
Re: Sorting from a database [In reply to] Can't Post

It depends how your are building the array which you want to sort. If each entry in @Array is a reference to an anonomous array which contains the team's name then goals then points, do it like this:

Code
# team 1 name, goals for team 1, points for team 1, team2, goals2, points2... 
@Stats = ("Arrows", "35", "30", "Bills", "40", "20", "Flukes", "40", "8", "Timbits", "85", "22", "Wacky", "85", "22");
While (@Stats) {
push(@Array, [shift(@Stats), shift(@Stats), shift(@Stats)]);
}
@Stats = sort { $b -> [1] <=> $a -> [1] || $b -> [2] <=> $a -> [2] || $a -> [0] cmp $b -> [0] } @Array;

After the sort, @Stats contains,

Code
$Stats[0] = ["Timbits", "85", "22"]; 
$Stats[1] = ["Wacky", "85", "22"];
$Stats[2] = ["Bills", "40", "20"];
$Stats[3] = ["Flukes", "40", "8"];
$Stats[4] = ["Arrows", "35", "30"];

which you can print out with:

Code
foreach (@Stats) { 
print "$_ -> [0], $_ -> [1], $_ -> [2]\n";
}

The printout will look like:

Code
Timbits, 85, 22 
Wacky, 85, 22
Bills, 40, 20
Flukes, 40, 8
Arrows, 35, 30

Cool I threw in a second sub-sort which sorts names alphabetically only if two teams have the same number of goals and points.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives