Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I handle mid-line carriage returns in a flatfile database?

( #6510=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 30, 2000 at 09:07 UTC
Q&A  > regular expressions


Description:

I'm using a flat file database and getting problems when carriage returns (asc 0x0D) occur in the middle of lines. This happens, for example, when the text comes from a web form TEXTAREA field. How can I strip such carriage return characters from a string?

Answer: How do I handle mid-line carriage returns in a flatfile database?
contributed by cianoz

Usually i prefer to uri-encode multiline fields so i can preserve LF and CR... you can do this way:

use URI::Escape; my $safe_field = uri_escape($field); ## now you can put $safe_field in the db ## without worry about CR/LF a ## restore it: $field = uri_unescape($safe_field);
Answer: How do I handle mid-line carriage returns in a flatfile database?
contributed by btrott

I believe you should be able to just do something like this:

$entry =~ tr/\r//d;
This will strip every carriage return from $entry. Alternatively, you may wish to replace carriage returns with spaces (so that you don't get words running together):
$entry =~ tr/\r/ /;
I'd recommend the latter.
Answer: How do I handle mid-line carriage returns in a flatfile database?
contributed by extremely

The best way is to stop using return to end database records. =) Set $/="\033\n"; and write records with that on the end of the record. Using a 2 char line terminator lets you have a nice return at the end for eyeballing the files and use a nasty binary character to make the end-of-line special versus your data.

Answer: How do I handle mid-line carriage returns in a flatfile database?
contributed by gryphon

While this is probably not the best way to go about this, the following has worked for me on many an occation:

use CGI qw(header param); print header; foreach (param) { ($$_ = param($_)) =~ s/\r\n|\n/<BR>/g; }

Note: This won't work with strict, and therefore probably shouldn't be used as is. Something a little safer might be:

(my $some_variable = param('textbox-name')) =~ s/\r\n|\n/<BR>/g;

The key thing is that depending on the user's OS, browser client, or other unknown something, you might end up with your params having just /\n/ or /\r\n/ in them. (Blame Micro$oft. Works for me.) ;) When I'm storing stuff in a flat-file "database," I'll usually store carriage returns as either /<BR>/ or /\\n/ and have Perl, JavaScript, or whatever deal with converting it to something else later.

Shoot me an email if this doesn't help or answer what you're looking for.

Answer: How do I handle mid-line carriage returns in a flatfile database?
contributed by Anonymous Monk

There's a whole bunch of stuff in the FAQ about matching over more than one line, like the /s modifier for the end of a regular expression, which means "the stuff you find doesn't have to be on the same line".

It also say that if you set $/ = '' then Perl will read in paragraphs at a time, not lines at a time.

Does that help?

But obviously, killing the carriage returns on the way in to your program is going to solve it. The equivalent to the $/ = '' thing above is surely to

* replace all double returns with a holding pattern like ||||

* replace all single returns with spaces

* replace all |||| with returns

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2014-11-23 22:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (134 votes), past polls