in reply to Regular expressions across multiple lines
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: <%-{-{-{-<
|
---|