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

Greetings Monks,

I am trying to write a sequence of files out. I seem to have run into a problem, where all of the records end up in the same file. I am not sure why this is happening. Granted it is the first time I am trying to do something like this. As always your help is greatly appreciated.

I call the sub with two values; the first value is the first two digits of an account code. A sample account would look like: 01-B4 123456 and a tabbed record that I want written to a file that would look similar to: 1\t2\t20071107\t01-B4 123456\tPAID\t91.40

All the records appear in the file, it ‘s just they all end up in the same file. I was expecting the directory to look something like:
msg_beta_4250.txt
msg_beta_4450.txt
msg_beta_1240.txt

Any thoughts or recommendations on what I might look for to correct his behavior?

push(@passVars, $passCompCode); push(@passVars, $passLogRecord); write_msg_File(@passVars); sub write_msg_File { my $compCodeStr = $_[0]; my $logDataStr = $_[1]; my $logCompCode; $logCompCode = get_Comp_Code($compCodeStr); my $outFile = "/root/acs/out/msg_beta_" . "$logCompCode" . "\.txt"; #my $outFile = "/home/clr/log/msg" . "$logCompCode" . "\.txt"; open (WRECORD, ">> $outFile") or warn "Cannot Write Record! - $!"; print WRECORD $logDataStr; close (WRECORD); system "chown apache:apache $outFile"; system "chmod a=rw $outFile"; } #sub sub get_Comp_Code { my $companyRef = $_[0]; $companyRef =~ s/^0*//; my @companyArray = ("0000","4250","4450","3943","3943","4095","4241 +","1240","3943","4250","3943","1850","1850","4450","4450","1900","185 +5","1855","1855","1855","1863","1872","1855","1865","1855","1870","18 +55","3436","0000","1389","1780","0000","1872","3943","1864","3438","0 +000","1864","1864","1864","1240","1240","1240","1551","1392","1392"," +0000","1391","1451","0000","1397","4450"); if (($companyRef >= 52) || ($companyRef <= -1)) { return "0000"; } # if my $companyCode = $companyArray[$companyRef]; return $companyCode; } #sub
Update, bad copy and paste.
Before perlmonks.org: perl -e "print scalar(localtime);" After perlmonks.org : perl -e "use strict; use warnings; print scalar(localtime).\"\n\";"

Replies are listed 'Best First'.
Re: How to write a sequence of files?
by moritz (Cardinal) on Nov 08, 2007 at 18:21 UTC
    open (WRECORD, ">> $outFile") or warn "Cannot Write Record! - $!"; close (WRECORD);
    You open that file for appending - and then close it immediately, without writing to it.
      I have corrected it sorry about that.
      Before perlmonks.org: perl -e "print scalar(localtime);" After perlmonks.org : perl -e "use strict; use warnings; print scalar(localtime).\"\n\";"
Re: How to write a sequence of files?
by QM (Parson) on Nov 08, 2007 at 18:15 UTC
    Where do you write to the file?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      That was a bad copy and paste on my part, sorry about that.
      Before perlmonks.org: perl -e "print scalar(localtime);" After perlmonks.org : perl -e "use strict; use warnings; print scalar(localtime).\"\n\";"
Re: How to write a sequence of files?
by graff (Chancellor) on Nov 09, 2007 at 02:48 UTC
    All the records appear in the file, it ‘s just they all end up in the same file.

    And what is the name of that file? Is the four-digit "company code" present or absent in the file name? You have not shown us how values get assigned to your "$passCompCode" variable, and the problem may just be that this variable's value is never changing.

    The code seems like it would do the right thing if given distinct values for that variable. One thing I would change, though, in the "get_Comp_Code" sub:

    if ( $companyRef > $#companyArray or $companyRef < 0 ) {
    The point is that the condition depends on the size of the array, not some "magic number" like "52".