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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.