in reply to Array Searching

How many web addresses is a "whole lot"?

For instance, how long does it take to run this:

#!/usr/bin/perl -w use strict; my @addresses = qw|http://www.foo.com http://www.foo1.com http://www.f +oo2.com http://www.foo.com http://www.a.com http://www.a.com http:/ +/www.a.com http://www.a.com|; my %add; for(@addresses){$add{$_}++}; print sort {$add{$b} <=> $add{$a}} keys %add; 1;
Update: LOL. ikegami rewrote my code, but do you realize that now this routine will sort the same 100K+ list twice just to get the top 10? I mean, heck, neither of our answers is really correct when you think about the volume...perhaps the data should be pushed to a database instead?

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: Array Searching
by ikegami (Patriarch) on Mar 09, 2006 at 20:01 UTC
    • Reverted names back to those the OP uses.
    • Switch to pre-increment for speed boost in some perls.
    • Answer both questions, not just one.
    • Made input list more readable.
    • Made printed list more readable.
    • Removed extraneous 1;.
    use strict; use warnings; my @sites = qw| http://www.foo.com http://www.foo1.com http://www.foo2.com http://www.foo.com http://www.a.com http://www.a.com http://www.a.com http://www.a.com |; my %sites; ++$sites{$_} foreach @sites; my @validsites = sort { $a <=> $b } keys %sites; my @popularsites = sort { $sites{$b} <=> $sites{$a} } keys %sites; splice(@popularsites, 10); # Keep only the 10 most popular. { local $, = "\n"; local $\ = "\n"; print 'Most Popular Sites', '------------------', @popularsites, '', 'All Sites', '---------', @validsites; }
Re^2: Array Searching
by cptstarfire (Initiate) on Mar 09, 2006 at 19:58 UTC
    a whole lot is about 100,000.