in reply to Opening a File Twice
You're not closing the file from your first step and hence some (or rather, all) lines remain in memory, buffered by Perl or the OS. close the first file or use a lexical filehandle (or both), and you will see the data when you reopen the file.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Opening a File Twice
by Jamin (Novice) on May 28, 2008 at 14:25 UTC | |
At least I think I am. Here is a shortened version of the code:
| [reply] [d/l] |
by apl (Monsignor) on May 28, 2008 at 14:39 UTC | |
to Then you could replace with The value of $_ is almost certainly used for more than the name of the file being processed. These changes will guarantee the filename is retained, and associate the actual filename with the error. Revise: The foreach was corrected, thanks to moritz kindly pointing out my gaffe. | [reply] [d/l] [select] |
by Corion (Patriarch) on May 28, 2008 at 15:14 UTC | |
With Perl 5.8.5, I can't reproduce your error. I've changed your code around a bit to make it work with strict and removed some of the file tests because they are redundant but still: outputs for me:
Maybe your assignment to $contents is somehow wrong? More debugging information is needed. | [reply] [d/l] [select] |
by starbolin (Hermit) on May 28, 2008 at 19:57 UTC | |
I don't see where your problem has anything to do with the ordering of opens and closes. More likely the records are not getting written correctly. Your code slurps the contents of the input file as one record into the first element of @array_contents then writes $contents out to the second file without any record delimiters. The code provided does nothing when I run it. Have you verified the contents of your written file using a text editor? Can you provide a snippet of code that behaves as you say? Two other minor points. You call chdir() inside your foreach(@files) loop. This will only work for absolute path names otherwise the code will try to chdir to a subdirectory from inside a subdirectory. The $count loop displays the first record as a count of 0. While it would be proper to consider this an index of 0, the count should read 1.
s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo} | [reply] [d/l] |