Gossamer Forum
Home : General : Perl Programming :

Help with Sorting Routine for my script.

Quote Reply
Help with Sorting Routine for my script.
 

Code:
#!/usr/bin/perl
#

$targetnumba = 3 ;

open (HTML,">keyword.html");
print HTML"<HTML><HEAD><TITLE>Keyword List Html Build</TITLE></HEAD><body><center>";
print HTML"<br><br><a href=keyword.cgi>Build Keywords</a><br><br>";
print HTML"<table width=600 border=2 cellpadding=5 cellspacing=5><tr><td valign=top>Keyword</td><td valign=top>Number of Searches</td></tr>";
open (DATABASE,"<keywords.txt");
@database_in=<DATABASE>;

#foreach $_ (@database_in)
#{
#($name,$numba)=split(/\:/);
#if($numba > $targetnumba){
#print HTML"<tr><td valign=top><font color=RED><b><em>$name</font></em></b></td><td valign=top><font color=RED><b><em>$numba</font></em></b></td></tr>";
#}
#else {
#print HTML"<tr><td valign=top>$name </td><td valign=top><font color=BLUE>$numba</font></td></tr>";
#}
#}



while DATABASE {
push (@database_in, $_);
}
sort NumberSort (@database_in);
@result = split(/\:/,@database_in);
$name = $result[0];
$numba = $result[1];


if($numba > $targetnumba){
print HTML"<tr><td valign=top><font color=RED><b><em>$name</font></em></b></td><td valign=top><font color=RED><b><em>$numba</font></em></b></td></tr>";
}
else {
print HTML"<tr><td valign=top>$name </td><td valign=top><font color=BLUE>$numba</font></td></tr>";
}
}




print HTML"</center></table><br><br></body></html>";
close (HTML);
print "Content-Type: text/html\n\n";
print "<HTML><HEAD><TITLE>Keyword List Html Build</TITLE></HEAD><body link=red vlink=red bgcolor=ffffff text=000000><center>";
print "<br><br>";
print "Finished Sorting / Building Building Keywords HTML Page";
print "<br><br>";
print "<br><br>";
print "<a href=keyword.html>Go to Keywords</a>";
print "<br><br>";
print "<br><br>";
print "</center></body></html>";



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


I have some commented lines in there that worked with my previous script.. Now I'm trying to get it to sort the second field in the db , the number field , and print to the keyword.html in numerical order from HIGHEST to LOWEST number. I can't figure it out.. Can someone PLEASE help me!!! Wink

Thanx in Advance!

------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
Sort returns an array, it does not modify one, so:

Code:
sort NumberSort (@database_in);

does not do anything. Also you don't need the Number sort routine, <=> does the same thing. Try:

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

That should work.

Cheers,

Alex
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
 
Okay, here is my current code.. documented
and re-arranged a little better.


Code:
#!/usr/bin/perl
#
print "Content-Type: text/html\n\n";





$targetnumba = 3 ; # If greater than this number the script will highlight those entries.

##############
#
# Open Files for reading.
#
##############
open (HTML,">keyword.html");
print HTML"<HTML><HEAD><TITLE>Keyword List Html Build</TITLE></HEAD><body><center>";
print HTML"<br><br><a href=keyword.cgi>Build Keywords</a><br><br>";
print HTML"<table width=600 border=2 cellpadding=5 cellspacing=5><tr><td valign=top>Keyword</td><td valign=top>Number of Searches</td></tr>";
open (DATABASE,"<keywords.txt");


###########################
#
# Array, Loop and Sort. This is where my problem is. Its not sorting.
# I need it to SORT my db, which is a fat file : as the delimiter
# Each entry is on one line and looks like
# crowe:1
# you:1
# Blah:2
# cool:7
# Fun:9
# and so on.
# I want to sort, Highest number to lowest and print out both
# $name with corresponding $numba ..
# This still doesn't sort, but it does still write the file.. but
# as it is.. no sorting of any kind.
#
#############################

@database_in=<DATABASE>;
@database_in = sort { $b <=> $a } @database_in;


foreach $_ (@database_in)
{
($name,$numba)=split(/\:/);
if($numba > $targetnumba)
{
print HTML"<tr><td valign=top><font color=RED><b><em>$name</font></em></b></td><td valign=top><font color=RED><b><em>$numba</font></em></b></td></tr>";
}
else {
print HTML"<tr><td valign=top>$name </td><td valign=top><font color=BLUE>$numba</font></td></tr>";
}
}


# To here is the main code.


##################
#
# This Next part just Wraps up and Ends the HTML,
# Says that its done and links back to the file produced.
#
##################

print HTML"</center></table><br><br></body></html>";
close (HTML);
print "<HTML><HEAD><TITLE>Keyword List Html Build</TITLE></HEAD><body link=red vlink=red bgcolor=ffffff text=000000><center>";
print "<br><br>";
print "Finished Sorting / Building Building Keywords HTML Page";
print "<br><br>";
print "<br><br>";
print "<a href=keyword.html>Go to Keywords</a>";
print "<br><br>";
print "<br><br>";
print "</center></body></html>";


Its still not sorting.. Its just displaying
the file as IS. Man this sorting business is tough. I've read every doc I can find. I guess being a newby has limitations.. doah. Smile

I tried your solution alex ( and I also read about the way you told me in the Perl Cookbook ) and that seems like it should work. you can see it in action http://www.lit.org/key/keyword.html

I do appreciate the help.


------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
Ah.. The datafile looks like:

Zines:5
Comic Books:7
Cool:1
tree:1
Sex:1
Comics:1
Lit:1
Poetry:1
help:1
tale of two cities:1
charles dickens:1

so when you are sorting you are comparing:

'help:1' with 'Lit:1'

not what you want, you want to compare 1 with 1.

I would do this:

Code:
# ----------------------
open (DB, "<keywords.txt") or die $!;
while (<DB> ) {
chomp;
($name, $score) = split /:/;
$scores{$name} = $score;
}
close DB;
foreach $word (sort { $scores{$a} <=> $scores{$b} } keys %scores) {
print "$word => $scores{$word}\n";
}
# -------------------

What you have done is put the word, score into a hash %scores. So you can get any word's score by just doing $scores{$word}. The sort just sorts on the hash values.

Hope this helps,

Alex
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
Alex,

Your awesome. That did exactly it. Thanx a million for the help!!!!

I'll post the complete script in just a little bit ( its to go with the search log mod in links ) so that everyone can use it.


Thanx again!!!

------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
 

Code:
# ----------------------
open (DB, "<keywords.txt") or die $!;
while (<DB> ) {
chomp;
($name, $score) = split /:/;
$scores{$name} = $score;
}
close DB;
foreach $word (sort { $scores{$a} <=> $scores{$b} } keys %scores) {
print "$word => $scores{$word}\n";
}
# -------------------

Alex, is is possible to have it do that if like the db looks like this

cool:1:something
crazy:24:somethingelsehere
fun:3:anotherthinghere

I was fooling around with trying to add a bit of info to the script but I'm still not grasping this sort thing entirely.

Thanx again for the help..

------------------
The Crowe crowe@darkspiral.com
www.darkspiral.com
www.lit.org
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
Sure, just modify the split () call to look like:

($word, $score, @junk) = split /:/;

You just need to make sure you isolate the word and the score into their proper variables.

Cheers,

Alex
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
Okay, I'm still a bit lost. Let me go into more detail.

I have a db file. It looks like this

crowe:17:white:blue:grey
john:24 Wink

I appreciate all the info emmensily.




------------------
The Crowe crowe@darkspiral.com
www.darkspiral.com
www.lit.org
Quote Reply
Re: Help with Sorting Routine for my script. In reply to
I kinda goofed on my DB example Wink

Replace the RED faces with

:


..heh.

------------------
The Crowe crowe@darkspiral.com
www.darkspiral.com
www.lit.org