Nick wrote:

Hi There,

I am trying to analyze a simple log file and pull 2 pieces of data from it. The log looks as follows:

www.example.com 42f3ca10 42f8c42f 0 7338 0 3638

Where each valie is tab seperated. I want to create a hash with "www.example.com" as the key and column 5 (7338 in this example) as the value. There will be lots and lots of lines hence me using foreach.

This is how I am attempting it:

#!/usr/bin/perl

use Data::Dumper;

@tmp = `cat /usr/local/apache/logs/tmp`;

foreach (@tmp) {
if (/(^.+(\.net|\.uk|\.com|\.org))(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})(\t[a-z0-9]{9})/) {
        $bw_usage{$1} = ( $6 );
    }
}

print Dumper \%bw_usage;

Firstly, please don't laugh at my code too much! :o) Now I thought this

I think, "keep it simple" is a key rule most of the time. :)
If you have tab separated values, then spliting on \t should be very straightforward,

#untested
my %bw_usage;
for my $line (@tmp) {
  my ($k, $v) = (split /\t/, $line)[0,4];
  #domain test?
  next if $k !~ /\.(net|uk|com|org)$/i;

  $bw_usage{$k} = $v;
}
print Dumper \%bw_usage;


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to