Gossamer Forum
Home : General : Perl Programming :

Sorting

Quote Reply
Sorting
Ok, i have a plain text file logging all of my referrals like:

sitename|hits

i know howto read both of the fields into variables like $sitename and $hits, the only thing i need to know howto do is be able to sort the site's by there hits, like the most hits displayed first.

This is just for in my admin when i view them, i don't want to mess around and totally re-arrange the file itself but play around with the data in a table.

I know that there is a way i just need some help Smile

Thanks!
--Jacob
http://www.123webmaster.com
Quote Reply
Re: Sorting In reply to
1. it's better to have the plain text with $hits|$sitename!!!!!
2. you'll need the following code:
open..... (your file)
while .... {
push (@list, $_);
}
sort NumberSort (@list);
@result = split(/\|/,@list);
$hits = $result[0];
$sitename = $result[1];

3. you need this sub
sub NumberSort {
# ------------------------
# sort's numbers
if($a < $b) { return -1; }
elsif($a == $b) { return 0; }
else { return 1; }
}

thats it! should work now.
cu

------------------
www.ford-network.at