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

How can add a statement that says if $date_listed (sampledate: 05/29/03) changes write values to anoter textfile then reset all variables ($NumOfFiles,$PlacementDollar,$CurrBal) and start counting again until date changes agian and so on until End Of File.
$NumOfFiles = 0; $PlacementDollar = 0; $CurrBal = 0; $file = "rpt.dat"; # Name the file open(REPORT, $file); # Open the file while(<REPORT>) { my($line) = $_; # Puts opened File Into Array Line by Line foreach $line ($_) { chop($rptdata); # Define input data from FACS report ($accnt_num,$date_listed,$disposition,$client,$cancel_reason, +$amt_paid,$initial_bal,$accnt_bal)=split(/","/,$line); if ($disposition eq "3DL0" or $disposition eq "3DL1") { $NumOfFiles += 1; $PlacementDollar += $initial_bal; $CurrBal += $accnt_bal; } close(REPORT); # Close the file

Edit by tye, add CODE tags

Replies are listed 'Best First'.
Re: on Date Change Question
by jonadab (Parson) on Sep 16, 2003 at 23:21 UTC
    How can add a statement that says if $date_listed (sampledate: 05/29/03) changes write values to anoter textfile then reset all variables ($NumOfFiles,$PlacementDollar,$CurrBal) and start counting again until date changes agian and so on until End Of File.

    Just keep track of the last-read date in a variable, and each time through the loop compare. If it's different from the date you just read, do all the stuff you want and update the last-read date.

    while (<REPORT>) { my $line = $_; # Is this assignment really needed? ($accnt_num, $date_listed, $disposition, $client, $cancel_reason, $amt_paid, $initial_bal, $accnt_bal) =split /","/; if ($date_listed ne $last_date) { write_values_to_another_textfile(); reset_all_variables(); $last_date = $date_listed; } if ($disposition eq "3DL0" or $disposition eq "3DL1") { $NumOfFiles += 1; $PlacementDollar += $initial_bal; $CurrBal += $accnt_bal; } }

    Depending on your circumstances, you may want to reverse the order of the two if sections, so that the summing is done before the resetting. Or not. I'd have to understand more of the context of what you're doing to say for sure.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: on Date Change Question
by kutsu (Priest) on Sep 16, 2003 at 18:41 UTC

    Thanks tye

    don't know what your asking but might it be:

    if ($date_listed eq $date) or if ($date_listed =~ /$date/)

    might look at localtime if you need the date

    "Pain is weakness leaving the body, I find myself in pain everyday" -me