I am writing a little script that reads in a CSV file and stuffs each line into an array, as below:
[...] # Read in the file while (my $line = <FILE> ) { if ($csv->parse($line)) { @row = $csv->fields(); push @data, [ @row ]; } }
one row in @data looks about like this: (I produced the below line with this code:)
for ($i=0 ; $i < $lines; $i++) { for (my $j=0; $j < 6; $j++) { print "$data[$i][$j] "; } print "\n"; } QP 2/27/2008 555 1000.00 2493819320
Anyway, what I need to do is:
1) Generate the sum of element 3 where element 1 reoccurs in the array +. I.e., tally the day's sales. 2. Print out a line with the sum of the day's sales. 3. Print the row with each individual sale, with element 3 made negati +ve.
When I was merely producing one entry per row in the array, I used the code below, which worked just fine, but summing the sales by date is throwing me.
for ($i=0 ; $i < $lines; $i++) { $amount = $data[$i][3] * -1; print OUTFILE "some header information\n"; print OUTFILE "some more header info\n"; print OUTFILE "$data[$i][1]\t1350 Account\t$data[$i][3]\t$data +[$i][0]\t$data[$i][4]\n"; print OUTFILE "$trnsid\tt$data[$i][1]\t2600Other:$data[$i][2]\t$am +ount\t$data[$i][0] $data[$i][4]\n"; }

In reply to Silly array-ish question. by fleurdmiller

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.