
Kenosis
User
Mar 11, 2013, 3:21 PM
Post #2 of 2
(106 views)
|
|
Re: [showale] Need help in decoding the following PERL code
[In reply to]
|
Can't Post
|
|
This perl script's executed from the command line, reading each line of $testdata.emp and removes the end-of-record (a newline, in this case) from each line if the line doesn't end with "|". Output is directed to $testdata.emp.tmp Here's the regex:
!/\|$/ ^^^^^^ |||||| |||||+ - End match ||||+ - End of line |||+ - Verticle bar's escaped (so it's not regarded as alternation) ||+ - Escape |+ - Begin match + - Not the case So, the condition evaluates to true and the line is chomped if the line doesn't end with "|". In a script with __DATA__ :
use strict; use warnings; while (<DATA>) { if ( !/\|$/ ) { chomp; } print; } __DATA__ Line 1 Line 2| Line 3 Line 4| Line 5 Line 6| Output:
Line 1Line 2| Line 3Line 4| Line 5Line 6| Lines 1, 3 & 5 don't end with "|", so their ending newline was removed.
(This post was edited by Kenosis on Mar 11, 2013, 3:22 PM)
|