If you're dealing with log files, they're often large and copying all their data into arrays, and then creating more arrays from that data, is generally not a good idea: it's likely to be very slow and you could run into memory issues.
Instead, read the files one line at a time or, in your case here, one paragraph at a time. The following line (which you'll see in the script I've provided below) turns on paragraph mode:
local $/ = '';
I'll also just comment on all those do statements. Is there a reason you coded it that way? Instead of
do { STATEMENTS } if CONDITION;
why not write
if (CONDITION) { STATEMENTS }
Anyway, I truncated your data quite substantially but left the main features: head, multiline data and tail. The data you posted had a space after every "Receive:" — I've left that in but you should check if it's there in the original data (it could be an artefact of copy/paste, HTML rendering, etc.). Here's the script to process it:
#!/usr/bin/env perl use strict; use warnings; { local $/ = ''; my $wanted = "Receive: \n"; my $re = qr{(\[\d\d\]|.)}; while (<DATA>) { chomp; next unless substr($_, 0, length $wanted, '') eq $wanted; #print "|$_|\n"; # For demo only - see current $_ value my @hex; while (/$re/g) { push @hex, length $1 == 1 ? sprintf '%02X', ord $1 : substr($1, 1, 2); } print "@hex\n"; } } __DATA__ Message: 11 started at: 2018-06-29 16:20:07 Transmit: ATV1[0D] Transmit: [01]10179311000=[03] Receive: [01]20179321157>[02]00 00 00[03][00] Transmit: [01]10179312000>[03] Receive: [01]20179331157?[02]00 00 00[03][00] Message: amount of bytes collected ok, data accepted Message: 11 ended at: 2018-06-29 16:20:46
Output:
01 32 30 31 37 39 33 32 31 31 35 37 3E 02 30 30 30 30 30 30 03 00 01 32 30 31 37 39 33 33 31 31 35 37 3F 02 30 30 30 30 30 30 03 00
If you uncomment that demo print line, you'll probably find it a bit easier to compare the data being processed and the data being output. It changes the output to this:
|[01]20179321157>[02]00 00 00[03][00]| 01 32 30 31 37 39 33 32 31 31 35 37 3E 02 30 30 30 30 30 30 03 00 |[01]20179331157?[02]00 00 00[03][00]| 01 32 30 31 37 39 33 33 31 31 35 37 3F 02 30 30 30 30 30 30 03 00
— Ken
In reply to Re: how use map and grep or several loops?
by kcott
in thread how use map and grep or several loops?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |