Hello monks,

I have a communication log now like below

Message: 11 started at: 2018-06-29 16:20:07 Transmit: ATV1[0D] Transmit: [01]10179311000=[03] Receive: [01]20179321157>[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] Transmit: [01]10179312000>[03] Receive: [01]20179331157?[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] Transmit: [01]10179313000?[03] Receive: [01]201793411578[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] Transmit: [01]101793140008[03] Receive: [01]201793511579[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] Transmit: [01]101793150009[03] Receive: [01]001793611578[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] Message: reading of spontaneous buffer not ordered Message: Periodic Buffer: Start: 2018-06-29 13:15:00 End: 2018-06-29 16:10:00 Periods: 36 Dec: 8 Points: 15 bytes collected: 5564 estimated: 556 +4 Message: amount of bytes collected ok, data accepted Message: 11 ended at: 2018-06-29 16:20:46
the log has 3 sections: Message, Transmit and Receive. Every section compose of 3 parts: head(like Message:,Transmit:etc), data(mix hex char,ascii code, and return because every data can't be larger than 80 bytes so maybe one received data could be split to 2 or 3lines), and tail(only a return).

my job is retrieved all data in receive section, remove every return tailed fromdata, convertascii code tohex string,likebelow:
Receive: [01]201793511579[02]00000068801400000000000000000000000000000000006880 +14000000 0000000068801400000000000000000000400000000040000000004000000000400000 +00000000 000000000000000000[03][00] #old 01 32 30 31...........30 03 00 #newstring has only one +line, is allmade of hex string and no []
what I do is create 2 tempery array to deal with it:
my $start_str = "Receive"; my $end_str ="\n"; my @first_loop = grep { /$start_str/../^$end_str$/ ? 1 : 0; } @logs; my @second_loop; my $data = ""; my $k = 0;my $j=0; for(@first_loop){ #first loop is to rem +ove return, head and tail do { $k = 1, next } if $_ =~ /$start_str/; do { $k = 0; push @second_loop, $data; $data = ""; next;} if $ +_ =~ /^$end_str$/; do { $sctm_data .= $_; chomp $sctm_data; next } if $k == 1; } my @strings; my $refined_logs; my $hex_str; for(@second_loop){ # second loop is to convert and remo +ve [] my $data_str = $_; chomp $data_str; for(split(//, $data_str)) { do { $j = 1, next } if $_ =~ /\[/; do { $j = 0;push @strings, $hex_str; $hex_str = ""; next;} if +$_ =~ /\]/; do { $hex_str .= $_; next } if $k == 1; push @strings, sprintf("%02X", ord($_)); } my $last_str = join( " ", @strings ); @strings =(); push @refined_logs, $last_str; }
It works, but I'd like to only use map plus grep to deal with it. like @output_array = map{} map {} grep{} grep{} input_array. I think it's more tidy and easy understanding. Could you enlightened me using several examples? or in this senario, map and grep is not a good way? Thanks for your help!


In reply to 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.