Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I am trying to create a Top Site script, for those who do not know what this is , take a look at clipart.com
Most aspects of this are fairly simple to do , but there are a few things which are puzzling me, any help would be appreciated....

User data will all be on one line in a text file, one of the pieces of data will be the number of hits that site has brought in , how can i extract this from each line and get it to print them in order, highest to lowest.

I would also like to get the script to split the pages up , to say 20 accounts per page, i have no idea how to do this any advice would be appreciated.

Thanks a lot guys,
John

Replies are listed 'Best First'.
Re: Ranking numbers .....
by davorg (Chancellor) on Jul 29, 2000 at 18:30 UTC

    Difficult to give any specific help without knowing the format of your data file, but let's assume that you have a line with three pieces of data (url, name and hist count) separated by colons. You could then do something like this:

    open(DATA, 'hits.dat') or die "Can't open hits.dat: $!\n"; my @pages; push @pages, [split /:/] while <DATA>; my @sorted = sort { $b[2] <=> $a[2] } @pages;

    @sorted now contains a list of your pages sorted from highest to lowest hits. Each element contains a reference to an array. Each of these second level arrays contains three elements, the url, the name and the hit count. To display a certain subset of these, you'd need to have a parameter which tells you where in the list to start and you could then use splice to extract the pages that you need. Something like this perhaps:

    my $start = param('start'); my $display = 20; print map { "@$_\n" } splice @sorted, $start, $display);;
    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Ranking numbers .....
by elusion (Curate) on Jul 29, 2000 at 19:28 UTC
    The way I would do it would be to use sort to assign to an array for each site. Then take the number of hits from each site, and join it with the name of the site into another array, with the number first. That way you can sort the array and it will be the number of hits each site had in order. You can then recall it by using a scalar value for the Array. If that's not good enough you can use REGEX to match the name from the array with all the stats in the site's own array. Hope this makes sense,
    $i = 0; open(DATA,"text.txt"); foreach $line (<DATA>) { @site1 = split " ", $line; $master[$i] = "$site1[0], $site1[1]"; } sort @master; print "1. $master[0]"; print "2. $master[1]"; print "3. $master[2]"; print "4. $master[3]"; print "5. $master[4]";

    I know this doesn't quite work because of the name of the array for each site, but you should be able to figure that out for yourself.

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one."
    -Albert Einstein