Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I tried to output the chomped text to a txt file. When I open that text file in a text editor it shows weird overlapping text (like some sort of graphical problem).   [from this; emphasis added]

afoken has already covered the cross-system line-end mismatch and chomp problems pretty well. Here's a further example:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "dd $/; ;; my $s = qq{abc\r\n}; chomp $s; dd $s; ;; my @lines = (qq{Overlapping\r\n}, qq{Strange\r\n}, qq{Text\r\n}); dd \@lines; ;; chomp @lines; dd \@lines; ;; my $line = join '', @lines; dd $line; ;; print qq{$line}; " "\n" "abc\r" ["Overlapping\r\n", "Strange\r\n", "Text\r\n"] ["Overlapping\r", "Strange\r", "Text\r"] "Overlapping\rStrange\rText\r" Textngeping

The other thing that occurs to me is that your original file has "invisible" whitespace characters other than the  \r \n line-enders: spaces, tabs, etc. If these exist at the end of a line, it will cause a problem. Try something like (untested):
    open my $filehandle, '<', ... or die "...: $!";
    ...
    my $content = do { local $/;  <$filehandle>; };
    $content =~ tr{\x20\t\f\r\n}{}d;

In general, processing a file of a few hundred megabytes entirely held in memory should not be a big problem, depending on what you're doing, and you're basic approach (insofar as I can understand what it is) looks ok to me.

And, of course, the golden rule: Know Your Data!

Update: BTW:  my $content = do { local $/;  <$filehandle>; }; is the "file slurp" idiom; it does a raw read of the entire file to a string.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Regular expressions across multiple lines by AnomalousMonk
in thread Regular expressions across multiple lines by abcd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found