Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: how use map and grep or several loops?

by johngg (Canon)
on Jul 05, 2018 at 12:09 UTC ( [id://1217946]=note: print w/replies, xml ) Need Help??


in reply to how use map and grep or several loops?

Some might disagree with you regarding the clarity of the grep and map approach and a solution using loops of various flavours might be easier to maintain in the long run. However, here's a solution for you which wraps everything in a do block to contain the localization of paragraph mode.

use 5.022; use warnings; open my $logFH, q{<}, \ <<__EOD__ or die $!; 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 __EOD__ my @records = do { local $/ = q{}; map { join q{ }, map { m{\[\d\d\]} ? substr $_, 1, 2 : map { sprintf q{%02x}, ord } split m{}; } @$_; } map { s{^Receive:}{}; s{\s+}{}g; [ split m{(\[\d\d\])} ]; } grep { m{^Receive:} } <$logFH>; }; say qq{$_\n} for @records;

I don't show the output here as it will wrap horribly. Despite giving you this solution I would recommend sticking with looping constructs.

Update: Got rid of the middle map by moving the split into the first map.

Update 2: The first update would have made more sense if I'd included the original code so here it is :-

map { join q{ }, map { m{\[\d\d\]} ? substr $_, 1, 2 : map { sprintf q{%02x}, ord } split m{}; } @$_; } map { [ split m{(\[\d\d\])} ] } map { s{^Receive:}{}; s{\s+}{}g; $_; } grep { m{^Receive:} } <$logFH>;

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: how use map and grep or several loops?
by Anonymous Monk on Jul 06, 2018 at 02:25 UTC

    Many thanks johngg !

    although you recommend loop way to deal with something like this, I prefer map/grep way. It's like a FIFO queue, so that you can easily understand how original data transform to the data I want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217946]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found