w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Perl Parsing Script Issues

I think that you should use an anonymous hash to store the $data

use strict;
use warnings;
use Date::Parse;

my $data = {};

sub dateDiff{
  my ($d1,$d2) = @_;
  return (str2time($d2) - str2time($d1));
}

open(DATA,"data1.dat") || die $!;
while(my $l = <DATA>){
  chomp($l);
  if($l =~ m/^(S+)s+(Up|Down)s+(.*)$/){
    $data->{$1}->{$2} = $3;
  } else {
    # print the line with errors or log it
  }
}
close(DATA);

print "SITEID		STATE	DATE TIME			STATE	 DATE TIME			UP	TIME
";
foreach my $k1 (keys %{$data}){
  print $k1 . "	";
  if(defined($data->{$k1}->{"Up"}) &&
defined($data->{$k1}->{"Down"})){
    print "Up	" . $data->{$k1}->{"Up"} . "	Down	" .
$data->{$k1}->{"Down"} 
       . "	Up	" .
dateDiff($data->{$k1}->{"Up"},$data->{$k1}->{"Down"})."
";
  }else{
    # print the part line as you need
  }
}

The output is in seconds, you could use any of the epoch to datetime ways to see that seconds in other format if you need.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.