Gossamer Forum
Home : Products : Links 2.0 : Customization :

Top 10 mod - very simple & very UNDERSTABLE

Quote Reply
Top 10 mod - very simple & very UNDERSTABLE
I made this after strandardt Top 10 sub don't show me one string which should be showed. Standardt sub build_rate_page very comlex for understand so I made myself:

sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.

my (@values, $id, $votes, $rate, @top_votes, @top_rate);
local ($top_rated, $top_votes);

if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}

$total = 0;

open (LINKS, $db_links_name) or &cgierr ("unable to open links database: $db_links_name. Reason: $!");
LINE: while (<LINKS>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
$votes = $values[$db_votes];
$rate = $values[$db_rating];
next if ($votes < 10);
if (($#top_votes < 9) or ($votes > $$top_votes[$#top_votes])) {
push (@top_votes, [$votes,@values]);
if ($#top_votes < 9) {
@top_votes = sort { $$b[0] <=> $$a[0] } @top_votes;
}
else {
shift(@top_votes);
}
}
if (($#top_rate < 9) or ($rate > $top_rate[$#top_rate])) {
push (@top_rate, [$rate,@values]);
if ($#top_rate <= 9) {
@top_rate = sort { $$b[0] <=> $$a[0] } @top_rate;
}
else {
shift(@top_rate)
}
}
}
close LINKS;

$top_rated = ''; $top_votes = '';

foreach (sort { $b <=> $a } @top_votes) {
$top_votes .= qq~<tr><td align=center>@{$_}[$db_rating+1]</td><td align=center>@{$_}[$db_votes+1]</td><td>
<a href="@{$_}[$db_url+1]">@{$_}[2]</a></td></tr>\n~;
}
foreach (sort { $b <=> $a } @top_rate) {
$top_rated .= qq~<tr><td align=center>@{$_}[$db_rating+1]</td><td align=center>@{$_}[$db_votes+1]</td><td>
<a href="@{$_}[$db_url+1]">@{$_}[2]</a></td></tr>\n~;
}
open (RATE, ">$build_ratings_path/$build_index") or &cgierr ("unable to open top rated page: $build_ratings_path/$build_index. Reason: $!");
print "\tVote Range: $top_votes[0][0] .. $top_votes[$#top_votes][0]\n";
print "\tRate Range: $top_rate[0][0] .. $top_rate[$#top_rate][0]\n";
print RATE &site_html_ratings;
close RATE;
}

If anyone would be test and find error please tell me.
Quote Reply
Re: [toypaul] Top 10 mod - very simple & very UNDERSTABLE In reply to
hmmm, I just did a quick and dirty replace on this and:

on the top vote getters: it left out the two top votegetters ( well, #2 and #3 both had the same number of votes and it left one of them out.

on the top vote getters: showed 9 instead of 10 ( see above problem )

on the top vote getters: not sorted in any particular order

on the highest rated: not sorted in any particular order

if i wanted to display another field, how do I do that? Just use

Code:
@{$_}[$db_fieldname+1]

I like it...! Just get it to sort and to add the top vote getters and I'll use it....I may test it more later when I have time....

Thanks




Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Top 10 mod - very simple & very UNDERSTABLE In reply to
Tnanks for notice.

A little bug creep into my code. So I made one more step to simpify Top 10 code. Just replace:

foreach (sort { $b <=> $a } @top_votes) {
...
foreach (sort { $b <=> $a } @top_rate) {

to

foreach (@top_votes) {
...
foreach (@top_rate) {

These array was already sorted - so don't needed to sort it again

Good luck!
Quote Reply
Re: [toypaul] Works Almost In reply to
At least this one works ... almost

The sort is now OK for the top rated.

The sort is not correct for the top vote getters. It is correct for the first 8 links then last two are not in sequence

Two links are missing. The top rated by votes lists 9 links but does not include the top two vote getters.

Notice the two in red in the top rated that are missing from the top vote getters

Building Top Rated Page . . .
Vote Range: 22 .. 11
Rate Range: 10.00 .. 8.93
Done


Code:
Top 10 Cities (by Rating) -- with at least 5 votes

Rating # Votes City
10.00 14 Charlotte
9.92 12 Alabama
9.90 10 Knoxville
9.81 10 Daytona
9.78 22 Charleston
9.68 22 Jacksonville
9.64 31 Atlanta

9.50 10 Sacramento
9.25 12 Hartford
8.93 15 Sacramento

--------------------------------------------------------------------------------

Top 10 Cities (by Votes) -- with at least 5 votes
Rating # Votes City
9.78 22 Charleston
8.93 15 Sacramento
9.25 12 Hartford
9.92 12 Alabama
9.81 10 Daytona
9.50 10 Sacramento
9.90 10 Knoxville
10.00 14 Charlotte
9.64 11 Orlando



Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Works Almost In reply to
Sorry I observe it before but forgotten to tell about it.

Just replace

if ($#top_votes < 9) {

to

if ($#top_votes <= 9) {

Once a little bug bring up not little problem ;)

P.S.
I think that is right correction but I can't to check it. Check yorself and tell me. I can only reflect so:

When two 22 is become last element become 9
and condition not correct. But in this case first element (11) must be erased but 22 still placed. And so later. So 22 and 31 must on top but 11 and 14 must be erased. I don't understand.

Quote Reply
Re: [esm] Works Almost In reply to
Oopps ...

Now I seems that I find right decision.
No it's exactly right decision.
I missing that first element in array is larger.
So it's needed delete element not from begin but from end.

Right code (and more simple ;):

if (($#top_votes < 9) or ($votes > $$top_votes[$#top_votes]))
{
if ($#top_votes == 9)
{
#first delete leaved element
pop(@top_votes);
}
# and add new
push (@top_votes, [$votes,@values]);
# place new element
@top_votes = sort { $$b[0] <=> $a[0]} @top_votes;
}

if (($#top_rate < 9) or ($rate > $top_rate[$#top_rate]))
{
if ($#top_rate == 9)
{
pop(@top_rate)
}
push (@top_rate, [$rate,@values]);
@top_rate = sort { $$b[0] <=> $$a[0] } @top_rate;
}
Quote Reply
Re: [toypaul] Getting Close! In reply to
 
OK here is the print out - need to sort the Votes!

Top 10 (by Rating) -- with at least 5 votes

Rating # Votes City
10.00 14 Charlotte
9.92 12 Alabama
9.90 10 Knoxville
9.81 10 Daytona
9.78 22 Charleston
9.68 22 Jacksonville
9.64 31 Atlanta
9.50 10 Sacramento
9.25 12 Hartford
8.93 15 Sacramento

--------------------------------------------------------------------------------

Top 10 (by Votes) -- with at least 5 votes
Rating # Votes City
9.64 31 Atlanta
9.90 10 Knoxville
9.25 12 Hartford
9.50 10 Sacramento
10.00 14 Charlotte
9.68 22 Jacksonville
9.81 10 Daytona
9.78 22 Charleston
9.92 12 Alabama
8.93 15 Sacramento

Here is the code

Code:
sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.
my (@values, $id, $votes, $rate, @top_votes, @top_rate);
local ($top_rated, $top_votes);

if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}

$total = 0;

open (LINKS, $db_links_name) or &cgierr ("unable to open links database: $db_links_name. Reason: $!");
LINE: while (<LINKS>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
$votes = $values[$db_votes];
$rate = $values[$db_rating];
next if ($votes < 10);
if (($#top_votes < 9) or ($votes > $top_votes[$#top_votes])){
if ($#top_votes == 9){
#first delete leaved element
pop(@top_votes);
}
# and add new
push (@top_votes, [$votes,@values]);
# place new element
@top_votes = sort { $$b[0] <=> $a[0]} @top_votes;
}

if (($#top_rate < 9) or ($rate > $top_rate[$#top_rate]))
{
if ($#top_rate == 9)
{
pop(@top_rate)
}
push (@top_rate, [$rate,@values]);
@top_rate = sort { $$b[0] <=> $$a[0] } @top_rate;
}
}
close LINKS;

$top_rated = ''; $top_votes = '';

foreach (@top_votes) {
$top_votes .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a class=mblue href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
<td align=center>@{$_}[$db_city+1]</td>
</tr>\n~;

}
foreach (@top_rate) {
$top_rated .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a class=mblue href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
<td align=center>@{$_}[$db_city+1]</td>
</tr>\n~;

}
open (RATE, ">$build_ratings_path/$build_index") or &cgierr ("unable to open top rated page: $build_ratings_path/$build_index. Reason: $!");
print "\tVote Range: $top_votes[0][0] .. $top_votes[$#top_votes][0]\n";
print "\tRate Range: $top_rate[0][0] .. $top_rate[$#top_rate][0]\n";
print RATE &site_html_ratings;
close RATE;
}


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Getting Close! In reply to
It's inpossible. Think yorself. The code for votes and for rates is the same. I took your votes sequence and enter in my links.db - result is OK. May be you will send yours links.db and category.db (in zip archive) and I will test on my comp?
Quote Reply
Re: [toypaul] Getting Close! In reply to
OK! You did a a great job! You should post this as a mod in the resource center. The original build rate page did not work.

There was an small error in the last change you made.

Code:
# place new element
@top_votes = sort { $$b[0] <=> $a[0]} @top_votes;

should be

Code:
# place new element
@top_votes = sort { $$b[0] <=> $$a[0]} @top_votes;

Since we cannot edit previous messages in this new forum software, here is the code that works ( well, OK, we can edit for 30 minutes after posting - that might be a great feature for general forum but not one devoted to programming!!!! ).

Code:
# MOD to sub build_rate_page
sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.
my (@values, $id, $votes, $rate, @top_votes, @top_rate);
local ($top_rated, $top_votes);

if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}

$total = 0;

open (LINKS, $db_links_name) or &cgierr ("unable to open links database: $db_links_name. Reason: $!");
LINE: while (<LINKS>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
$votes = $values[$db_votes];
$rate = $values[$db_rating];
next if ($votes < 10);
if (($#top_votes < 9) or ($votes > $top_votes[$#top_votes])){
if ($#top_votes == 9){
#first delete leaved element
pop(@top_votes);
}
# and add new
push (@top_votes, [$votes,@values]);
# place new element
@top_votes = sort { $$b[0] <=> $$a[0]} @top_votes;
}

if (($#top_rate < 9) or ($rate > $top_rate[$#top_rate]))
{
if ($#top_rate == 9)
{
pop(@top_rate)
}
push (@top_rate, [$rate,@values]);
@top_rate = sort { $$b[0] <=> $$a[0] } @top_rate;
}
}
close LINKS;

$top_rated = ''; $top_votes = '';

foreach (@top_votes) {
$top_votes .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a class=mblue href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
<td align=center>@{$_}[$db_city+1]</td>
</tr>\n~;

}
foreach (@top_rate) {
$top_rated .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
</tr>\n~;

}
open (RATE, ">$build_ratings_path/$build_index") or &cgierr ("unable to open top rated page:
$build_ratings_path/$build_index. Reason: $!");
print "\tVote Range: $top_votes[0][0] .. $top_votes[$#top_votes][0]\n";
print "\tRate Range: $top_rate[0][0] .. $top_rate[$#top_rate][0]\n";
print RATE &site_html_ratings;
close RATE;
}

Great job to toypaul





Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Getting Close! In reply to
Have u guy'z got a site where one can see this mod in action???...

ed

Less Talk More Action: www.9o9o.com
Quote Reply
Re: [edpak] Getting Close! In reply to
Greet thanks to esm Wink

Without your help I am nothing. Gene is name of women or man. How bad is my english? I am russian guy.

I have sitehttp://1csql.udmnet.ru where is mod work, but it only start and very small and in Top 10 nothing records. Also I have a lot off mods for: nonenglish mod (so simplify), diffrent templates of link an category for differnt categoies (it's worked). May be I will compose this mods to one packet and then public (in my site or resource center)

esm tell me please how I can public this mod in resource centre
Quote Reply
Re: [esm] Getting Close! In reply to
I understand. My code (on my comp) was free from this bug. So I tell you that on my comp it's all right.
Quote Reply
Re: [toypaul] Getting Close! In reply to
Your english is good enough to understand 95% of it...and is much better than my Russian...!

Gene can be either man or woman but I am a male.

And I don't know much Perl....so I had to guess a little with some of your changes. You might try using the code and b pushbuttons at the bottom of the screen where you type in your message to highlight sections of your text. Click on the Get Markup Help link just above the row of icons when you are typing in a message for more help.

As as Perl novice, I like to have the before and after code....show me what you want to change and then show me what it should look like after the change....like I did when I pointed out the missing $.

Great work....! The original Top 10 did not work and this one does. Glad I could do a little testing for you. If I can help with testing other stuff, send me an email at esm2006@yahoo.com

To post this to the resource center, click on a link to the the Support page and then click the Resource link. Here is the link: http://gossamer-threads.com/scripts/resources/

I have a Russian friend Sergei living in Chicago. Now I have another Russian friend...! Nice...!



Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [edpak] Getting Close! In reply to
it works....it looks just like the original except it prints 10 links for each category instead of something less than that...

just replace the build_rate_page routine with the correct post and it will work. I did not have to make any other changes. Except to the section of code that prints the links...I added another field and used some css stuff to format the link itself....

I spent some time with the original and could never get it to print more the 6-8 links. This one has printed 10 each time so far and does observe the minimum number to make the list. And toypaul seems to understand what he is doing and worked to make the small corrections needed to finish it up.



Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Getting Close! In reply to
Thanks!

I will public this mod in resource centre

and I can send you some mod about I wrote in previsious message
Quote Reply
Re: [toypaul] Getting Close! In reply to
A few months ago I help a liitle with the above top rated mod, but it has become apparent that there is still a bug in the code. Actually maybe, two...

First from the table below, it appears that the code stops after it gets 10 in the list. Then ( the second error ) it uses the same list to resort it for the top voters ( or at least, loops thru the same way ) - the list contains the links in both

3 10008 9.67 33
6 10011 9.40 10
5 10021 9.47 13
7 10037 8.87 15
9 10154 8.80 10
10 10160 8.33 12
8 10162 8.84 13
2 10194 9.80 19
4 10226 9.63 19
1 10259 9.92 13
- 10261 9.64 11
- 10396 6.80 10
- 10415 10.00 15
- 10448 10.00 10


Can any one shed any light on the code below???
Code:
# MOD to sub build_rate_page
sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.
my (@values, $id, $votes, $rate, @top_votes, @top_rate);
local ($top_rated, $top_votes);if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}$total = 0;open (LINKS, $db_links_name) or &cgierr ("unable to open links database: $db_links_name. Reason: $!");
LINE: while (<LINKS>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
$votes = $values[$db_votes];
$rate = $values[$db_rating];
next if ($votes < 10);
if (($#top_votes < 9) or ($votes > $top_votes[$#top_votes])){
if ($#top_votes == 9){
#first delete leaved element
pop(@top_votes);
}
# and add new
push (@top_votes, [$votes,@values]);
# place new element
@top_votes = sort { $$b[0] <=> $$a[0]} @top_votes;
}

if (($#top_rate < 9) or ($rate > $top_rate[$#top_rate]))
{
if ($#top_rate == 9)
{
pop(@top_rate)
}
push (@top_rate, [$rate,@values]);
@top_rate = sort { $$b[0] <=> $$a[0] } @top_rate;
}
}
close LINKS;$top_rated = ''; $top_votes = '';foreach (@top_votes) {
$top_votes .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a class=mLink href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
<td>@{$_}[$db_city+1]</td>
</tr>\n~;}
foreach (@top_rate) {
$top_rated .= qq~
<tr>
<td align=center>@{$_}[$db_rating+1]</td>
<td align=center>@{$_}[$db_votes+1]</td>
<td><a class=mLink href="@{$_}[$db_url+1]">@{$_}[2]</a></td>
<td>@{$_}[$db_city+1]</td>
</tr>\n~;}
open (RATE, ">$build_ratings_path/$build_index") or &cgierr ("unable to open top rated page: $build_ratings_path/$build_index. Reason: $!");
print "\tVote Range: $top_votes[0][0] .. $top_votes[$#top_votes][0]\n";
print "\tRate Range: $top_rate[0][0] .. $top_rate[$#top_rate][0]\n";
print RATE &site_html_ratings;
close RATE;
}


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Getting Close! In reply to
anyone have any ideas on fixing this problem?


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."