use strict; use warnings; use Data::Dumper; sub readlogfile { my ($filename) = @_; open my $logfile, '<', $filename or die "open $filename: $!"; my (@lookup, $entry, $trap); while (<$logfile>) { chomp; if ( /^Date:\s+(.+)$/ ) { push @lookup, ($entry = { DATE => $1 }); undef $trap; } elsif ( !$entry ) { next; } elsif ( /^(SServer|Device|CString|Port):\s+(.+)$/ ) { $entry->{uc $1} = $2; } elsif ( /^\s+(Trap OID|Generic|Specific|Varbind|Type):\s+(.+)$/ ) { $trap->{uc $1} = $2; } elsif ( /^\s+Data:\s+(.+)$/ ) { push @{$entry->{TRAPS}{TRAP_LIST}}, $trap if $trap; $trap = { DATA => $1 }; } } return @lookup; } print Dumper readlogfile("logfile.txt");