Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Substituting Newline Characters

by wolfi (Scribe)
on Mar 16, 2004 at 04:54 UTC ( [id://336929]=note: print w/replies, xml ) Need Help??


in reply to Substituting Newline Characters

i read the Q a little different than my other, fellow monks did - so, i'm gonna answer it this way.(Can't hurt. Someone may need the info, regardless.)

the way i take it, is that you're reading from say - a flatfile (.txt) database - trying to swap out the perl in favour of html, so it can be read by a browser or otherwise converted to html. In other words, you're not exchanging "newlines" but the perl command-like (meta-)characters that represent newlines (\n).

if i'm not mistaken...
perl considers the / / in a regex the same as it does double-quotes " " - which means, they're interpolated (carry their full worth - variables are substituted for their values, etc). So, exchanging \n would be exchanging an actual newline - rather than newline characters.

(i know, it's confusing.)

anyways...
if i were exchanging "\n" for "<br>\n", i'd make sure, that the characters were all turned off ("just characters") and not something with more meaning.

like...

my $comments =~ s/\\n/\<\.br\>\\n/;

that extra slash before the special (meta) character tells perl to "turn the special meaning off" (or rather "escape the character" in perl lingo.)

or, i believe, single-quotes, which don't interpolate - might work as well -

my $comments =~ s/'\n'/'\n<.br>\n'/;

(i almost never use that method, so can't be 100% sure, how well it works.)

in short... if you have a text file, that's got actual tabs, line-breaks, etc in them - try regexes joost and tachyon are suggesting. If you have in the file the actual characters...

hi\t my name is Count Dracula\n

use this.

update...just for the record, i don't think the angle brackets <> need to be escaped. (oops)

Replies are listed 'Best First'.
Re: Re: Substituting Newline Characters
by bkiahg (Pilgrim) on Mar 16, 2004 at 15:44 UTC
    Thanks Guys!

    The g is what I was missing. Although the other code examples were excellent food for thought.

    Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://336929]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-25 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found