[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Peter's Perl Predicament



Hello Mac Perlers,

I had some help from Matthias in getting this script to work (Thankyou
Matthias).
It reads the access log from my Web Site (Log), and the list of document
names (File Base) of docs on the site. It works fine.
(Page down for the problem)


#!/usr/local/bin/perl -w
$a='0';
$d='0';
$c='0';


$base='Int HD:Desktop Folder:File Base';
open(INFO, $base);                            #open the list of file names
@bas=<INFO>;                                  #call it @bas
close(INFO);

$file='Int HD:Desktop Folder:Log';
open(INFO, $file);                            #open the log of hits
@lines=<INFO>;                                #call it @lines
close(INFO);

$stats='Int HD:Desktop Folder:stats';         #open a file to take the results
open(INFO, ">>$stats");

foreach $bas(@bas)                            #run the sub for each file name,
{                                             #print the result and go on to the
 chop $bas;                                             #next one

&eachline;

print INFO "For the file             $bas\n
Total hits           $a
New Zealand   hits   $d
International hits   $c
----------------------------------------------------------------------------
--\n";
}

print "DONE !!";

#################
sub eachline
{
foreach $line(@lines)
{

if ($line=~/GET\shttp:\/\/www2.waikato.ac.nz:81$bas"\s|GET\s$bas\s/)
    {
++$a;
         {
          if ($line=~/nz\s/)
                {
                    ++$d;              #New Zealand
                }
          if ($line!~/nz\s/)
                {
                       ++$c;           #international
                }
          }
     }
}
}
#################

However, Matthias said this :-

>instead of
>reading a fixed file of Bas'es, you could simply grep the actual URLs i.e.
>
>
>if (/GET (http://[^/]+)?(\S+)/) {
>    if (.nz) {
>        $NZHits{$2}++;
>    } elsif (.au) {
>        $AUHits{$2}++;
>    } else {
>        $Worldhits{$2}++;
>    }
>}
>
>
>and then after you've processed all lines
>
>foreach $url (sort keys %NZHits) {
>...
>}
>
>Matthias

This is the bit I don't understand, I've tried various things without success.
I am a complete beginner, and have no programming background. I've started
this Web Site and all of a sudden the logs are each several hundred pages
long (and growing !),
and so I have been learning Perl over the last few weeks so that I can
extract the necessary stats from the logs. If I could achieve what Matthias
has outlined it would make life a lot easier. I have searched high and low
for the answer but all the documentation I can find tells me some but not
all of what I need to know. What I need is for some kind soul to take me
step by step through the process of "grepping the actual urls" so that I
can see how it's done.
I have ordered 'Learning Perl' from  O'Reilly's but it hasn't arrived yet.
If anyone can spare the time to help me out here I'd be very grateful.

Regards
Peter Matthews.
http://www.ohlnet.co.nz/ohlnet/