Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

combining values from multiple lines

by mmittiga17 (Scribe)
on Apr 14, 2009 at 15:17 UTC ( [id://757410]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I am stuggling with creating a script that will take the value from a field on each line where the account number is the same and add them together. There will never be more than two lines per account number. End result would be to take the value in field 5 where acct# number is the same and print out 1 new line for each Acct# with the combined value. I can get the acct#, cusip and values. Just cant seem to figure out how to add them together. Any help will be greatly appreciated. Changed code a bit, problem I get now is $recs{$ACTNUM} += $CSH_TRD_BAL2 ; increments. Even if Actnum only exist once the value is incorrect. I want to get print out the actnum once with the combined value of $CSH_TRD_BAL2 for a single account number: ACTNUM,VALUE
my %recs =(); while (<inData>) { chomp; $line = $_; next unless ($line =~/^DTL/); $CUSIP = substr $line, 4, 12; $CUSIP =~s/\s+//g; $ACTNUM = substr $line, 240, 9; $CSH_TRD_BAL = substr $line, 64, 17; $CSH_TRD_BAL2 = substr $line, 64, 17; $recs{$ACTNUM} += $CSH_TRD_BAL2 ; &GetCusip("$CUSIP"); print "$recs{$ACTNUM} $CSH_TRD_BAL2 $CUSIP $ACTNUM\n " if ($CUSCOD +E eq 'MMKS'); }

Replies are listed 'Best First'.
Re: combining values from multiple lines
by kennethk (Abbot) on Apr 14, 2009 at 15:32 UTC
    Please see How (Not) To Ask A Question; in particular, it's challenging to debug a parsing script without input data.

    In a more general sense, if you use a hash keyed on account number, you can just use that value as an accumulator, i.e.

    my %values_hash = (); while (<inData>) { chomp; # parse lines $values_hash{$ACTNUM} += $value; }

    or something like that...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://757410]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found