appending to a global filehandle

M

markpark

i have a function called writedata and, inside the function, some
things
get calculated ( details not important ) and then a file gets opened
and written to. but this function
is called over and over and what i want to do is keep appending to the
same file.
but, it seems like using the >> doesn't help because the files becomes
empty
once the routine is exited ?

is there some way to deal with this ? I am very much a novice in perl
and
much of the code below was cut and pasted form other people's programs.
i definitely don't want to
give the false impression that i know what i am doing.

i guess that i need to make the filehandle global somehow but i'm not
clear on how to do that ?
or maybe send it in as a parameter ? thank you very much for any help
that can be provided.

# MAIN PROGRAM ( not quite but this gives the idea )

foreach (my $bin=$exchMap->{$Exch}{s2}; $bin<=$exchMap->{$Exch}{e2};
$bin++) {
my $cMap={};

writedata($date,$cnt,$Exch,$bin,$cMap,$bMap,$prevMap,$ewmaMap,$cnddir);
$cnt++;
}

# END OF MAIN PROGRAM


sub writedata {
my $date = shift;
my $cnt = shift;
my $exch = shift;
my $bin = shift;
my $cMap = shift;
my $bMap = shift;
my $prevMap = shift;
my $ewmaMap = shift;
my $cnddir = shift;

my $outf = $cnddir . "/$exch" . "." . "dailyfile.txt";

open (OUTF,">> $outf") || die "could not open $outf";

foreach my $xid (keys %{$cMap}) {
if (! exists($bMap->{$xid})) { next; }

my ($te,$code) = getPrimary($xid,$date);

my $ca = $cMap->{$xid}{ca};
my $cb = $cMap->{$xid}{cb};
my $cmid = ($ca+$cb)/2;
if ($ca<=0 or $cb<=0 or $cb > $ca) { next; }
my $ccp = $cMap->{$xid}{ccp};
my $totvs = $cMap->{$xid}{totvs};
my $hp = $cMap->{$xid}{hp};
my $lp = $cMap->{$xid}{lp};

my $ba = $bMap->{$xid}{ca};
my $bb = $bMap->{$xid}{cb};
my $bmid = ($ba+$bb)/2;
if ($ba<=0 or $bb<=0 or $bb > $ba) { next; }

my $pr = 0;
my $pvs = 0;
if (exists($prevMap->{$xid})) {
$pr = $prevMap->{$xid}{ret};
$pvs = $prevMap->{$xid}{totvs};
}

my $r = sprintf("%.6f",log($cmid/$bmid));
my $deltar=sprintf("%.6f",$r-$pr);
my $deltavs=sprintf("%.0f",$totvs-$pvs);

my $ewma2 = sprintf("%.6f",fetchEWMA($xid,$deltar,2,$ewmaMap));
my $ewma5 = sprintf("%.6f",fetchEWMA($xid,$deltar,5,$ewmaMap));
my $ewma10 = sprintf("%.6f",fetchEWMA($xid,$deltar,10,$ewmaMap));
my $ewma20 = sprintf("%.6f",fetchEWMA($xid,$deltar,20,$ewmaMap));
my $ewma30 = sprintf("%.6f",fetchEWMA($xid,$deltar,30,$ewmaMap));
my $ewma40 = sprintf("%.6f",fetchEWMA($xid,$deltar,40,$ewmaMap));
my $ewma50 = sprintf("%.6f",fetchEWMA($xid,$deltar,50,$ewmaMap));
my $ewma60 = sprintf("%.6f",fetchEWMA($xid,$deltar,60,$ewmaMap));
my $ewma90 = sprintf("%.6f",fetchEWMA($xid,$deltar,90,$ewmaMap));
my $ewma120 = sprintf("%.6f",fetchEWMA($xid,$deltar,120,$ewmaMap));

print OUTF
"$xid,$te,$code,$ca,$cb,$ccp,$hp,$lp,$totvs,$r,$deltar,$deltavs,$ewma2,$ewma5,$ewma10,$ewma20,$ewma30,$ewma40,$ewma50,$ewma60,$ewma90,$ewma120\n";

$prevMap->{$xid}{ret}=$r;
$prevMap->{$xid}{totvs}=$totvs;
}

close(OUTF);
}
 
J

John W. Krahn

markpark said:
i have a function called writedata and, inside the function, some
things get calculated ( details not important ) and then a
file gets opened and written to. but this function is called over
and over and what i want to do is keep appending to the same file.
but, it seems like using the >> doesn't help because the files becomes
empty once the routine is exited ?

No, using the mode '>>' does *NOT* empty the file. Perhaps some other code in
your program is emptying the file?

is there some way to deal with this ? I am very much a novice in perl
and much of the code below was cut and pasted form other people's
programs. i definitely don't want to give the false impression that
i know what i am doing.

i guess that i need to make the filehandle global somehow but i'm not
clear on how to do that ?

Filehandles are package variables so they are visible throughout the current
package (main).

or maybe send it in as a parameter ?

perldoc -q "How do I pass filehandles between subroutines"




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top