Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't substitute Newline in text

Status
Not open for further replies.

zzapper

Programmer
Oct 30, 2008
6
0
0
GB
Hi
I am reading a text field from an XML feed. When I view that field in various ways eg with vi I can see the plain text is formatted in lines and paragraphs. So i want to substitute newlines for say 'fred'. I've tried the following with no success

$p_desc=~s/\n/fred/g;
$p_desc=~s/\n\r/fred/g;
$p_desc=~s/^$/fred/g;
$p_desc=~s/^ *$/fred/g;

Can you help? Should I be splitting?
 
try:

$p_desc =~ s/\n/fred/sg;

assumes you have multiple lines stored in the scalar.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi

I would say the problem is with the order of substitutions :
Code:
$p_desc=~s/\n/fred/g;    # eliminate \n
$p_desc=~s/\n\r/fred/g;  # searching for more \n
$p_desc=~s/^$/fred/g;    # searching empty lines (?) but line delimiters were removed previously
$p_desc=~s/^ *$/fred/g;  # similarly
(?) - assuming you intended to use the [tt]m[/tt] modifier.

Feherke.
 
I think those are examples of everything he has tried, but maybe not.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this works for me:

Code:
foo = qq{this is a test
this is a test
this is a test
};
$foo =~ s/\n/fred/sg;
print $foo;

output:

this is a testfredthis is a testfredthis is a testfred

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
should be $foo in the first line of code above.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi

Oops. I supposed (s)he done all four substitutions one after another. Otherwise I do not understand the problem, as that substitution works for me without [tt]s[/tt] modifier too :
Code:
$foo =~ s/\n/fred/g;

Feherke.
 
Yea, it does, it works with or without the "s". So I am also confused at this point. Seeing some of the actual data may help.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi
Thanks y'all it turns out my problem that in the xml feed the newlines were being transmitted as actual or literal \n (where \n is two ascii characters)
so I needed

$p_pdesc =~s/\\n/<br>/sg;

What I don't understand is how previously there were being translated into real \n newlines when I spat the result into mysql?





 
Without seeing your code we couldn't tell why MySQL would turn a literal \n (\\n) into a \n line break, but I run into something similar when outputting JavaScript from within Perl, like

Code:
print qq~
document.onLoad = function() {
   window.alert("hello\\nworld!");
}~;

which outputs JavaScript code

Code:
document.onLoad = function() {
   window.alert("hello\nworld!");
}

which pops up an alertbox saying

Code:
hello
world

In your case it might also be possible that MySQL understands the literal \n and translates it into a line break automatically (i.e. if you were typing commands at a SQL command prompt, you might type in a literal "\n" in one of the values and MySQL would have to know to turn it into a line feed).

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top