|
Posted by Mumia W. (on aioe) on December 18, 2006, 8:17 pm
Please log in for more thread options
On 12/18/2006 10:03 AM, hollyhawkins wrote:
> I have a script that reads a directory of all files, and creates an
> output file of one file with the contents of each individual file from
> the directory. The input is an HL7 file. In each individual file, the
> segments were separated with hex "0D", but now the segments have hex
> "0A" as the separator. This is causing the perl script to read each
> segment as a new line, and insert hex "0D0A" at the end.
>
>
> => QUESTION: How can I read in each individual file with all the data,
> and write it out as one chunk, as opposed to reading each segment
> ending with x"0A" and writing out each line to the output??? Any help
> would be greatly appreciated
> [...]
Set $/ to undef and put the file handle into "raw" mode:
... do some stuff ...
local $/ = undef;
open (my $X, '<', $filename) or die("Horribly: $!\n");
binmode($X, ':raw');
... do more stuff ...
Or you could just use File::Slurp as «perldoc -q "all at once"» suggests:
This code is UNTESTED!
use strict;
use warnings;
use File::Slurp;
#define the file directory paths
my $datainpath = "C:\YNHH_Files\Quest\Quest_IN";
my $dataoutpath = "C:\YNHH_Files\Quest\Quest_OUT";
my $tempdir = "C:\YNHH_Files\Quest\temp";
my $archivedir = "C:\YNHH_Files\Quest\archive";
#read the names of the individual files into an array "@allfiles"
my @allfiles = read_dir($datainpath);
if (@allfiles < 1) { exit }
#writes the names from the directory to a file in tempdir
@allfiles = grep !/99999dummyrec\.txt/, @allfiles;
write_file "$tempdir\recnames.txt", @allfiles;
unlink "$dataoutpath\questout.txt";
# Open questout.txt for the data from the records.
# Append => 1 will cause data to be appended.
# Binmode => ':raw' will prevent spurious \x0A characters
# from being written to the file.
foreach my $filename (@allfiles) {
my $data = read_file $filename;
write_file "$dataoutpath\questout.txt",
{ append => 1, binmode => ':raw' }, $data;
}
# What, we're done already?
# You DID post to comp.lang.perl.modules .
# WARNING: The above is untested code.
--
paduille.4060.mumia.w@earthlink.net
http://home.earthlink.net/~mumia.w.18.spam/
|