in reply to File opening, based on a regex...
You're misusing the regular espressions. Here is your read/write loop corrected to what I think you're looking for,
That doesn't really split your file, it just prints "Success" over each .txt file mentioned in the one you read. To actually split, you need to tell perl how to spot the end of the file, while printing $_ to *FILE.while(<>){ print if m/^-/; if (m/^(\w+\.txt)/){ open (local(*FILE), "> $1") or die $!; print FILE "Success"; close FILE or die $!; } }
Update: Ah, in that case I'll explain a bit more. The parenthesis in m/^(\w+\.txt)/ captures the matched text (here a number of 'word' characters followed by '.txt') and makes it available in the $1 variable. So 'foo.txt' at the start of the line matches, and $1 will contain that file name.
I assumed a little different file format and intent than Plankton did. You should adjust what we say to fit your needs. Added some error checking.
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: File opening, based on a regex...
by Mrdini (Initiate) on Feb 03, 2004 at 22:28 UTC |