ignore new line for long string

N

ngoc

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
 
A

Anno Siegel

Stephen Hildrey said:
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
 
S

Stephen Hildrey

ngoc said:
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
 
C

ced

ngoc said:
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,
 
N

Noname

Anno said:
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
 
A

Anno Siegel

Noname said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top