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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Sorting Array and Parsing a string


New User

Feb 10, 2011, 4:59 PM

Post #1 of 3 (926 views)
Sorting Array and Parsing a string Can't Post

I have two questions. I have a program that i need to take in a web log file and arrange in into some data. One thing I'm trying to do is sort the web addresses into the top 10 most requested sites. I'm using the following code to sort through how many hits they have had.
my %counts = ();
for (@site) {

This gives me the output of the number of hits each site has had, but I need to sort them from most requested to least, and only list the top 10.

Second, i need to take in a string like 21/Jun/2000 and parse it into a date so it can be compared within a week function. I don't want to use the Date::manip function or anything like that however.

Any help with these would be great.


Feb 10, 2011, 6:02 PM

Post #2 of 3 (921 views)
Re: [Zamereon] Sorting Array and Parsing a string [In reply to] Can't Post

The function that you want to accomplish is:

my @sorted_sites = sort {$counts{$b} <=> $counts{$a} } keys %counts; 
foreach my $site ( @sorted_sites[0..9] ) {
print $site, ' ', $counts{$site}, "\n";

This may be too slow if the number of sites is large. There are ways to speed it up, but this is probably ok.

Note: It is very difficult to get dates right without modules. It is even harder to prove that they are right. Use the module!
Good Luck,

New User

Feb 10, 2011, 6:50 PM

Post #3 of 3 (914 views)
Re: [BillKSmith] Sorting Array and Parsing a string [In reply to] Can't Post

I figured out how to get the dates to work. I just changed them into epoch time and did it that way.

And your code works perfectly. Thank you so much! :)


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

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