blacksmith has asked for the wisdom of the Perl Monks concerning the following question:

I am going to try my best to explain the issue I am confronting. I am pulling a file off of a network drive(directory) in which I must separate, rename by the PO number in the different sections, and then send the different sections with their new names to another directory in which they will be picked up by a fax server to be processed. I am having a little trouble following the replies to the post How do I split a file into parts. I have been able to make different files in the other directories (sometimes with data, sometimes without), but I can never get the name of the file right. I also need to delete out several "control characters" put into the file by the mainframe when it places it on the network drive. The code that has evolved so far is :
use strict my $source = "c:/ftproot/$report"; my $fil_count = 0; my $delim = 'FAX#'; open IN, "c:/ftproot/$report" or die "Can't open $report: $!\n"; open OUT, ">$filename" or die "Can't write to $filename: $!\n"; while (<IN>) { if (/^(.*?)$delim(.*)$/) { seek (OUT,2,1); read (OUT, my $num,4); my $fax = join("",$num,"F",$fil_count,".txt"); my $apollo="c:/reports/$fax"; my $daym=(localtime(time()))[3]; my $afis3="c:/afis3too/$daym/$fax"; print OUT $1 if $1; foreach my $dest ($truth, $afis3, $apollo,$zenos, $faxarc) { copy (<IN>, $dest); } close OUT; $fil_count++; open OUT, "> $filename" . $fil_count . ".txt" or die "Can't write to out${fil_count}.txt: $!\n"; print OUT $2 if $2; } else { print OUT $_; } } close IN; close OUTPUT; unlink $source;
I must apologize if this looks messy. I have not even begun to try to tackle the control characters yet. I just want to make sure that I can split the file up and assign the correct filenames to them. Before I forget. The beginning of each segment of the file begins with FAX#, two spaces after that begins 4 bytes that need to be the start of the filename. I hope that helps to clear up a little. Any advice (except to "just give up" 'cause that ain't happenin') would be appreciated. Blacksmith.

Replies are listed 'Best First'.
Re: Segmenting a file &deleting control characters
by bikeNomad (Priest) on Jun 19, 2001 at 21:46 UTC
    This bit is odd, for a file that was opened for write:
    seek (OUT,2,1); read (OUT, my $num,4);
    Why don't you just use a regex to pick out the segment markers?
    if (/^(.*?)$delim\s\s(....)(.*)$/) {
    Now the last bit of your last file is in $1, and the four characters to make into a filename are in $2. The start of your next file is in $3.

    Look into the tr/// operator for deleting control characters.

Re: Segmenting a file &deleting control characters
by Anonymous Monk on Jun 19, 2001 at 23:59 UTC
    while(<IN>){ ... copy(<IN>,$dest); ... }
    is wrong. <IN> is not a variable.
    I don't want to say more, because thinking is good for intelligence.
      I agree. Thinking is good for intelligence AND survival. But let's not forget that taking responsibility for a mistake is good for character. Having said that I did make that mistake, because I had pasted the code without verifing that line. Thanks for noticing. Blacksmith.