CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [showale] Need help in decoding the following PERL code: Edit Log



Kenosis
User

Mar 11, 2013, 3:21 PM


Views: 337
Re: [showale] Need help in decoding the following PERL code

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:

Code
!/\|$/  
^^^^^^
||||||
|||||+ - 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__ :

Code
use strict; 
use warnings;

while (<DATA>) {
if ( !/\|$/ ) {
chomp;
}
print;
}

__DATA__
Line 1
Line 2|
Line 3
Line 4|
Line 5
Line 6|


Output:

Code
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)


Edit Log:
Post edited by Kenosis (User) on Mar 11, 2013, 3:22 PM
Post edited by Kenosis (User) on Mar 11, 2013, 3:22 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives