deleting empty lines from a variable containing \n's

Discussion in 'Perl Misc' started by Tad McClellan, Nov 3, 2004.

  1. machoq <> wrote:
    > I read a file and store it in a scalar variable...the file contains
    > lines with ### i need to delete those lines. When i substitute them i
    > get an empty line but not a deletedline. How do i achieve this ?



    By deleting the newline character too.


    > open(FILE,"someFile");



    You should always, yes *always*, check the return value from open():

    open(FILE, 'someFile') or die "could not open 'someFile' $!";


    > $file =~ s/^#+$//mg;



    $file =~ s/^#+\n//mg;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 3, 2004
    #1
    1. Advertising

  2. Tad McClellan

    machoq Guest

    I read a file and store it in a scalar variable...the file contains
    lines with ### i need to delete those lines. When i substitute them i
    get an empty line but not a deletedline. How do i achieve this ?

    Code

    undef $/;
    open(FILE,"someFile");
    $file = <FILE>;
    $file =~ s/^#+$//mg;
    print $file;

    Result
    If someFileContained
    1) myCode
    2) ###
    3) furtherCode

    The output is
    1) myCode

    2) furtherCode

    I want output like
    1) myCode
    2) furtherCode

    regards
    -Machoq
    machoq, Nov 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    596
    Craig Deelsnyder
    Oct 25, 2003
  2. Harry Barker
    Replies:
    2
    Views:
    513
    Alf P. Steinbach
    Apr 19, 2006
  3. Umesh
    Replies:
    8
    Views:
    456
    Charlton Wilbur
    May 30, 2007
  4. mike
    Replies:
    3
    Views:
    95
  5. crea
    Replies:
    2
    Views:
    406
    Nobody
    Dec 28, 2012
Loading...

Share This Page