What if we could declare a new copy of my %frame each time we detect the start of a new frame record?
We wouldn't need to initialize %info = (). We wouldn't need to test if (%info). We could store \%frame instead of the clever but busy { %info }.
All it takes is loop labels:
# detect start of new data record my $FRAME_BEGINNING = qr/^Frame /; # store parsed Frame records my @frame_list; SKIP: while (<DATA>) { next SKIP if !/$FRAME_BEGINNING/; FRAME: while (!eof) { my %frame; DETAIL: while (<DATA>) { last DETAIL if /$FRAME_BEGINNING/; if (/^ *Source port: (\S+)\s+(\S+)/) { $frame{source_port} = $1; # also save $2? } if (/^ *Destination port: (\S+)\s+(\S+)/) { $frame{dest_port} = $1; } if (/^[\da-f]{4}\s+[\da-f\s]+/i) { push @{$frame{data}}, $_; # save data lines } } push @frame_list, \%frame; } }
In reply to Better scoping for the hash: was Re^2: Extracting data from a WireShark log
by Narveson
in thread Extracting data from a WireShark log
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |