#!/usr/bin/perl -w use strict; use Time::Local; my @ListOfRecords = (); #a list of hash while () { next if /^\s*$/; #skip blank lines my %record=(); my ($meter,$date,$time,$text) = split; my $epoch =dateTime2Epoch($date,$time); my ($code,$description)= $_ =~ /(\d+)_(\w+)/; #demo of a "hash slice... @record {"Meter","Epoch","Code","Descript"}= ($meter, $epoch, $code, $description); push (@ListOfRecords, \%record); } #### # just an example dump # could be sorted before this # could be written in fewer lines # this is just an example #### raw_dump(\@ListOfRecords); sub raw_dump { my $Loh=shift; foreach my $href (@$Loh) { foreach my $record (keys (%$href)) { if ($record eq "Epoch") { print "Date Time = ".epoch2datetime($href->{$record}) ."\n"; } else { print "$record=$href->{$record}\n"; } } print "\n"; } } #$epoch=dateTime2Epoch("09-11-2007", "15:27:30"); sub dateTime2Epoch { my ($date,$time) = @_; my($day,$month,$year)= split(/-/,$date); my($hour,$min,$sec)=split (/:/,$time); my $epoch_time = timegm($sec,$min,$hour,$day,$month-1, $year-1900); return $epoch_time; } sub epoch2datetime { my $epoch = shift; my($seconds, $minutes, $hours, $days, $month, $year) = gmtime($epoch); my $str_date_time =sprintf("%02d-%02d-%04d %02d:%02d:%02d", $days,$month+1,$year+1900, $hours, $minutes, $seconds); return ($str_date_time); } # prints: # Code=102 # Descript=Low_Alarm # Meter=000.000.002.249-2-P # Date Time = 09-11-2007 15:27:30 # # Code=102 # Descript=Low_Alarm # Meter=000.000.002.249-2-P # Date Time = 09-11-2007 16:44:18 # # Code=202 # Descript=Low_Repeat # Meter=000.000.002.249-2-P # Date Time = 09-11-2007 16:44:18 __DATA__ 000.000.002.249-2-P 09-11-2007 15:27:30 102_Low_Alarm 000.000.002.249-2-P 09-11-2007 16:44:18 102_Low_Alarm 000.000.002.249-2-P 09-11-2007 16:44:18 202_Low_Repeat