...open LOGFILE... my (@list, %info); while () { if (/^Frame /) { # detect start of new data record if (%info) { push(@list, { %info }) } # save last collected record %info = (); # start with blank record } elsif (/^ *Source port: (\S+)\s+(\S+)/) { $info{source_port} = $1; # also save $2? } elsif (/^ *Destination port: (\S+)\s+(\S+)/) { $info{dest_port} = $1; } elsif (/^[\da-f]{4}\s+[\da-f\s]+/i) { push(@{$info{data}}, $_); # save data lines } } if (%info) { # must check at end of loop push(@list, { %info }); } # @list contains parsed Frame records #### my $bytes; for my $line (@{$info->{data}}) { if ($line =~ m/^[\da-f]{4}\s*(([\da-f]{2} )+)/i) { my $hex = $1; $hex =~ s/\s//g; $bytes .= pack("H*", $hex); } }