keep new lines when printing scalar

Discussion in 'Perl Misc' started by yeti349@yahoo.com, Jul 21, 2005.

  1. Guest

    Hi,

    I am storing formatted data into a scalar and attemping to print it to
    a new file. The formatting, like new lines, is lost because everything
    is concatenated. How can i keep my new lines? Thank you.
     
    , Jul 21, 2005
    #1
    1. Advertising

  2. Stephen O'D Guest

    can you post some sample code? New lines etc are preserved when you
    store them in a scalar, unless you chomp them off:-

    $more testfile
    this
    is
    my
    test
    file
    contents

    -------------------------------------

    open ($f,"testfile")
    || die "failed top open file $!\n";

    my $data;

    while (<$f>) {
    $data .= $_;
    }

    open ($o,">newtestfile")
    || die "Failed to open the out file $!";

    print $o $data;

    close $f;
    close $o;

    ------------------------------------

    $ more newtestfile
    this
    is
    my
    test
    file
    contents

    The old and new files are identical ...
     
    Stephen O'D, Jul 21, 2005
    #2
    1. Advertising

  3. wrote in news::

    > I am storing formatted data into a scalar and attemping to print it to
    > a new file. The formatting, like new lines, is lost because everything
    > is concatenated. How can i keep my new lines? Thank you.


    Please read the posting guidelines for this group.

    See also <URL:http://perl.plover.com/Questions4.html> as
    well as item #11939 in

    <URL:http://groups-beta.google.com/group/comp.lang.perl.misc/msg/b2f911d6d9c5cef2?dmode=source>

    How do you expect us to help you if we don't have any idea
    what the code and the data look like?

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 21, 2005
    #3
  4. Joe Smith Guest

    wrote:

    > I am storing formatted data into a scalar and attemping to print it to
    > a new file. The formatting, like new lines, is lost because everything
    > is concatenated. How can i keep my new lines? Thank you.


    New lines are not lost when strings are concatenated, unless you have
    explicitly removed them. If you remove newlines, it is your
    responsibility to put them back in at the appropriate places.

    Are you using chomp()? Please post your code.
    -Joe
     
    Joe Smith, Jul 22, 2005
    #4
    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. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  2. Clint Olsen
    Replies:
    6
    Views:
    401
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    191
    Arndt Jonasson
    Jan 27, 2005
  4. Ciba LO
    Replies:
    4
    Views:
    198
    Ciba LO
    Jul 8, 2006
  5. hisan
    Replies:
    1
    Views:
    1,407
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page