OK, I've searched for this one and I'm coming up blank, so I submit to the Wiser Ones for help.
I am trying to split a text file into many smaller files, breaking it down by blank lines. I have isolated the problem to a split function, but I'm baffled why this wouldn't work.
The function looks like this:
{
open (FILE,"<$input_file") or die "Couldn't open file $input_file, $!\
+n";
local $/ = undef;
$slurp = <FILE>;
close FILE;
}
@data = split (/\n{2,}/,$slurp);
Once I've got the data into an array, the rest is cake and works fine.
Now, if I use an input file I create on a *NIX machine, it works flawlessly as written. But, if the input file comes from a Windows machine, it doesn't work.
Now before you go clicking "--" and screaming about newline conversion, let me tell you what I've tried...
If I first run the file through VI and do a
:%s/^$/-==-/g and then change the SPLIT to cut on that string, it works fine. If I change the SPLIT to
/^$/ it doesn't work, everything ends up in
@data[0]. I've also tried:
- split (/^\s+$/,$slurp);
- split (/\r\n\r\n/,$slurp);
- split (/\0x0D\0x0A/,$slurp);
- split (/(?:\cM\cJ){2,}/,$slurp);
- split (/^\s+$|^\n+$|^$/,$slurp);
...And a few others too desperate to mention. None of these have any affect on this intractable file, the whole file always ends up in
@data[0]. My CTS is acting up, so I'm probably missing something really obvious here, or there's some not-well-documented way to accomplish this on DOS/Windows text files that I can't figure out, but the really confusing thing is that vi can do it easy and Perl can't. Any clues???
Signature void where prohibited by law.
2001-04-05 Edit by Corion : Corrected title
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.