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: Advanced:
Ranking Items

 



mike
User

Mar 11, 2001, 8:21 PM

Post #1 of 10 (1690 views)
Ranking Items Can't Post

Hi,

I am trying to get this topsite script I made to rank items in the database according to hits in. However, I am a little confused as how to do so. The database reads like this:

Sitename|SiteURL|Hitsout|HitsIn

I want the list to rank the sites according to the number of traffic in. I have every aspect of the script working accept the ranking of the sites according to hits in. Any help would be appreciated.

Mike




rGeoffrey
User

Mar 11, 2001, 9:20 PM

Post #2 of 10 (1688 views)
Re: Ranking Items [In reply to] Can't Post

Take a look at this thread from the past or possibly this one.

Between them you should find the answer you are looking for.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


mike
User

Mar 11, 2001, 9:51 PM

Post #3 of 10 (1686 views)
Re: Ranking Items [In reply to] Can't Post

Hi again,

I looked at those posts and they only further confused me as to how to sort my sites according to their traffic in. I only want the script to sort them by their traffic in, I don't care about alphabetical order or anything. I just need to know how to make the script read my database file like this:

Sitename|SiteURL|Hitsout|HitsIn

and then display the sites in order of hits, and numbering them. If you could show me how to do this I would really appreciate it.

Mike



rGeoffrey
User

Mar 12, 2001, 9:32 AM

Post #4 of 10 (1681 views)
Re: Ranking Items [In reply to] Can't Post

The previous example wanted to sort data that looked like this...

xxxx, bbb, NNN123, xxxxxxxx

and used this code...


Code
my $SortField = 2; 
print OUTPUT map { $_->[1] }
sort { $a->[0] cmp $b->[0] }
map { [(split (',', $_))[$SortField], $_] }
<INPUT>;

You want to work with data like...

Sitename|SiteURL|Hitsout|HitsIn

and sort on HitsIn, which is $array[3] after the split.

So we need to change the split to work on a pipe not a comma
Make $SortField == 3
And change 'cmp' for string comparison to '<=>' for number comparison

Which means that this should do the job..


Code
my $SortField = 3; 
my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;

And to print the table we can make a few changes to the original and get...


Code
print ("\nThe sorted table for HitsIn is:\n<table>\n", 
"<tr><th>Order</th><th>Sitename</th><th>SiteURL</th>",
"<th>Hitsout</th><th>HitsIn</th></tr>\n",
);

my $ct = 1;
foreach my $row (@results) {
print '<tr>';
print "<td>", $ct++, "</td>\n";
foreach my $col (split ('\|', $row)) {
print "<td>$col</td>\n";
}
print "</tr>";
}

print "\n</table>\n";

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


mike
User

Mar 17, 2001, 12:12 PM

Post #5 of 10 (1663 views)
Re: Ranking Items [In reply to] Can't Post

Hi again.... ok this is what I put together:
_________________________________________

#!/usr/bin/perl


print "Content-Type: text/html\r\n\r\n";

print " The sorted table for HitsIn is: <table>";

open (DATA, "sites.txt");
@results = <DATA>;
close DATA;

my $SortField = 3;
my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;
print ("\n<tr><th>$Order</th><th>$Sitename</th><th>$SiteURL</th>",
"<th>$Hitsout</th><th>$HitsIn</th></tr>\n",
);

my $ct = 1;
foreach my $row (@results) {
print '<tr>';
print "<td>", $ct++, "</td>\n";
foreach my $col (split ('\|', $row)) {
print "<td>$col</td>\n";
}
print "</tr>";
}

print "\n</table>\n";

_______________________________________

For some reason it doesn't return anything except something that looks like this:
_______________________________________
The sorted table for HitsIn is: <table>
<tr><th></th><th></th><th></th><th></th><th></th></tr>

</table>
_______________________________________

I made a text file containing 4 sites and their data called sites.txt. If I did something wrong can you please help me.

Thanks,

Mike




rGeoffrey
User

Mar 18, 2001, 9:42 PM

Post #6 of 10 (1654 views)
Re: Ranking Items [In reply to] Can't Post

The problem is what you did to <DATA>...


Code
open (DATA, "sites.txt"); 
@results = <DATA>;
close DATA;

my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;

In the first three lines, you open a file, read all the lines into an array, and close the file. Then you try to read from the file as input to the map. But DATA has been closed, and even if you did not have the close line, you have already read all the lines out of it and <DATA> is empty.

You probably wanted to say...


Code
open (DATA, "sites.txt"); 
my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;
close DATA;

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


mike
User

Mar 19, 2001, 8:27 PM

Post #7 of 10 (1649 views)
Re: Ranking Items [In reply to] Can't Post

Hello,

OK, below is what I have so far. However, what is happening is it is still just printing out the sites and numbers in the same order they are in the file. Below is the script:

______________________________________

#!/usr/bin/perl


print "Content-Type: text/html\r\n\r\n";

print " The sorted table for HitsIn is: <table>";
print ("\n<tr><th>Order</th><th>Sitename</th><th>SiteURL</th>",
"<th>$Hitsout</th><th>HitsIn</th></tr>\n",
);

open (DATA, "sites.txt");
my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;
close DATA;

my $SortField = 3;

my $ct = 1;
foreach my $row (@results) {
print '<tr>';
print "<td>", $ct++, "</td>\n";
foreach my $col (split ('\|', $row)) {
print "<td>$col</td>\n";
}
print "</tr>";
}

print "\n</table>\n";

______________________________________

Also, this is what the sites.txt file looks like:

MikeSite|http://www.cgispy.com|242|23
WrestleWeek|http://wrestleweek.com|354|12
uFreebie|http://www.ufreebie.com|12|5
Bla bla bla|http://blah.com|24|3352




japhy
Enthusiast / Moderator

Mar 20, 2001, 12:23 PM

Post #8 of 10 (1646 views)
Re: Ranking Items [In reply to] Can't Post

The problem is you never defined $SortField, so it ends up acting like 0, and the 0th field is not a numeric one, so "string" <=> "string" ends up looking like 0 <=> 0.

Please turn on warnings in your program, and check the server's error log for them. #!/usr/bin/perl -w

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


mike
User

Mar 20, 2001, 3:52 PM

Post #9 of 10 (1643 views)
Re: Ranking Items [In reply to] Can't Post

Hello,

OK, everything works and it ranks the database, but it ranks the items from lowest to highest instead of highest to lowest. Like it puts the sites with the most traffic lower and the sites with less traffic higher. How do I make it rank them visa versa. The script now looks like this:

#!/usr/bin/perl


print "Content-Type: text/html\r\n\r\n";

print " The sorted table for HitsIn is: <table>";
print ("\n<tr><th>Order</th><th>Sitename</th><th>SiteURL</th>",
"<th>$Hitsout</th><th>HitsIn</th></tr>\n",
);

$SortField = 3;

open (DATA, "sites.txt");
my @results = map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [(split ('\|', $_))[$SortField], $_] }
<DATA>;
close DATA;



$ct = 1;
foreach my $row (@results) {
print '<tr>';
print "<td>", $ct++, "</td>\n";
foreach my $col (split ('\|', $row)) {
print "<td>$col</td>\n";
}
print "</tr>";
}

print "\n</table>\n";



Jasmine
Administrator / Moderator

Mar 21, 2001, 5:33 AM

Post #10 of 10 (1640 views)
Re: Ranking Items [In reply to] Can't Post

Change sort { $a->[0] <=> $b->[0] } to sort { $b->[0] <=> $a->[0] }


 
 


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

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