ignore new line for long string

Discussion in 'Perl Misc' started by ngoc, Aug 8, 2005.

  1. ngoc

    ngoc Guest

    Hi
    I have a very long string. Code will be ugly (not easy to navigate for
    reading) if I write it in a line.
    I want to write it in many lines, but treat as one line long string. How
    can I do it ?
    I used \ to escape new line, but it did not help.
    Thanks
    ngoc
     
    ngoc, Aug 8, 2005
    #1
    1. Advertising

  2. ngoc

    Anno Siegel Guest

    Stephen Hildrey <> wrote in comp.lang.perl.misc:
    > ngoc wrote:
    > > Hi
    > > I have a very long string. Code will be ugly (not easy to navigate for
    > > reading) if I write it in a line.
    > > I want to write it in many lines, but treat as one line long string. How
    > > can I do it ?

    >
    > code:
    >
    > use strict;
    > use warnings;
    >
    > (my $long = <<EOLONG) =~ tr/\n/ /;
    > This is a
    > really really really
    > long string
    > EOLONG
    >
    > print $long, "\n";
    >
    > ===
    >
    > output:
    >
    > This is a really really really long string


    What if the string is supposed to contain newlines?

    my $long = 'This is a' .
    ' really really really' .
    ' long string';
    print "$long\n";

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Aug 8, 2005
    #2
    1. Advertising

  3. ngoc wrote:
    > Hi
    > I have a very long string. Code will be ugly (not easy to navigate for
    > reading) if I write it in a line.
    > I want to write it in many lines, but treat as one line long string. How
    > can I do it ?


    code:

    use strict;
    use warnings;

    (my $long = <<EOLONG) =~ tr/\n/ /;
    This is a
    really really really
    long string
    EOLONG

    print $long, "\n";

    ===

    output:

    This is a really really really long string

    Hope that helps,
    Steve
     
    Stephen Hildrey, Aug 8, 2005
    #3
  4. ngoc

    Guest

    ngoc wrote:

    > I have a very long string. Code will be ugly (not easy to navigate for
    > reading) if I write it in a line.
    > I want to write it in many lines, but treat as one line long string. How
    > can I do it ?
    > I used \ to escape new line, but it did not help.


    If you mean breaking up a long, unwieldy string assignment,
    here are a few ways:

    my $string = "part1..." . "part2..." . "part3..." # using dot
    op.
    . "final part..."


    ( my $string = <<END ) =~ s/^(.*)\n/$1/mg; # using here doc
    part 1...
    part 2...
    part 3
    END

    my $string;
    $string .= $_ for ( "part1...", "part2...", # dot op. variant
    "part3 ... );

    hth,
    --
    Charles DeRykus
     
    , Aug 8, 2005
    #4
  5. ngoc

    Noname Guest

    Anno Siegel wrote:

    > Stephen Hildrey <> wrote in comp.lang.perl.misc:
    > > ngoc wrote:
    > > > Hi
    > > > I have a very long string. Code will be ugly (not easy to
    > > > navigate for reading) if I write it in a line.
    > > > I want to write it in many lines, but treat as one line long
    > > > string. How can I do it ?

    > >
    > > code:
    > >
    > > use strict;
    > > use warnings;
    > >
    > > (my $long = <<EOLONG) =~ tr/\n/ /;
    > > This is a
    > > really really really
    > > long string
    > > EOLONG
    > >
    > > print $long, "\n";
    > >
    > > ===
    > >
    > > output:
    > >
    > > This is a really really really long string

    >
    > What if the string is supposed to contain newlines?
    >
    > my $long = 'This is a' .
    > ' really really really' .
    > ' long string';
    > print "$long\n";
    >
    > Anno


    Then OP is supposed to use his head, Stephen tought him where to fish
     
    Noname, Aug 9, 2005
    #5
  6. ngoc

    Anno Siegel Guest

    Noname <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    >
    > > Stephen Hildrey <> wrote in comp.lang.perl.misc:
    > > > ngoc wrote:
    > > > > Hi
    > > > > I have a very long string. Code will be ugly (not easy to
    > > > > navigate for reading) if I write it in a line.
    > > > > I want to write it in many lines, but treat as one line long
    > > > > string. How can I do it ?
    > > >
    > > > code:
    > > >
    > > > use strict;
    > > > use warnings;
    > > >
    > > > (my $long = <<EOLONG) =~ tr/\n/ /;
    > > > This is a
    > > > really really really
    > > > long string
    > > > EOLONG
    > > >
    > > > print $long, "\n";
    > > >
    > > > ===
    > > >
    > > > output:
    > > >
    > > > This is a really really really long string

    > >
    > > What if the string is supposed to contain newlines?
    > >
    > > my $long = 'This is a' .
    > > ' really really really' .
    > > ' long string';
    > > print "$long\n";
    > >
    > > Anno

    >
    > Then OP is supposed to use his head, Stephen tought him where to fish


    Not really. He showed a solution using a here-document, which has no
    advantages over "" in the representation of long lines. Deleting the
    line-feeds after the fact is a possibility, but as I pointed out, it
    doesn't allow for the string to contain the occasional line feed, so
    it has restrictions.

    The standard solution is to concatenate the string from manageable
    pieces.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Aug 9, 2005
    #6
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    727
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,234
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    506
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    839
    Peter Nilsson
    Jan 15, 2008
  5. Rob Meade
    Replies:
    6
    Views:
    292
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page