Rodrick Brown wrote:
> Can someone take a look at the following sample code and let me know if you
> see any areas I can improve upon I tend to use the same structure when
> parsing txt files and before I commit this convention into memory maybe one
> of you guru's could let me know what I can improve upon.
> 
> Thanks
> 
> 
> #!/usr/bin/perl -w
> use strict;

use warnings;

> use Data::Dumper;
> 
> my $file = 'sample.txt';
> my ($ds,$time_v,$host);
> 
> open(my $fh, "<", $file ) or die("Fatal error unable to read $file: $!");
> 
> while(<$fh>) {

  chomp;

>   /(^[a-zA-Z].*)/ and $host = $1 and next;
>   /(^\d+)/ and $time_v = $1;
>   if ( defined $host and defined $time_v )
>   {
>     push @{$ds->{$host}},$time_v;

There is no need to keep an array of times if all you want is the total.

  if (/^[A-Z]/i) {
    $host = $_;
  }
  elsif (/(^\d+)/) {
    $ds->{$host} += $1 if defined $host;
  }

>   }
> }
> close ($fh);
> 
> #print Dumper($ds);
> 
> my $total = 0;
> 
> while ( my ($k, $v) = each(%{$ds}) ) {

Declare

  my $total = 0;

here

>   if ( ref $v eq 'ARRAY') {
>     foreach(@{$v}) {
>       $total +=$_;
>     }
>     print "$k: $total\n";
>     $total = 0;

and then there's no need to reset it.

>   }
> }

It would help us to see your data, but I hope this helps.

Rob

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


Reply via email to