Well, here's a little to get started ;-)

I am guessing, (from the code you have inherited), that you want the rows returned from the database query reported as columns - not rows. I wrote a transpose function for that. If that's not the case, then don't transpose @data.

This code could be a little sticky for a beginner, so ask questions on what you don't understand.

Note that the @col array contains the 'headers' of the data you want (in rows or columns, I don't know what is required).

These columns should be the names of the columns returned by the query (THAT you intend to be in the report) plus any additional fields you want to include, (mkt_1, profit, percent_profit, etc..).

my %ave12_cost; while (my @row = $sth_2->fetchrow_array) { # @row has 2 items - item AND average cost for the item # remove trailing spaces s/\s+$// for @row; # save item and average cost in a hash whose key is 'item' # 123.4 becomes 123.40 AND 456 becomes 456.00 $ave12_cost{ $row[0] } = sprintf "%.2f", $row[1]; } $sth_2->finish(); my @col = qw( agc market mkt_1 analysis_code item customer key_val2 key_val3 terms_code audit_seq aud_date aud_time opr_id action unit_price profit percent_profit cur_per_month ); my @data = [ @col ]; while (my $href = $sth_3->fetchrow_hashref) { # remove trailing spaces s/\s+$// for values %$href; # do calculations $href->{mkt_1} = substr($href->{market}, 0, 1) == 1 ? "A&D" : "MCP +"; $href->{profit} = $href->{unit_price} - $ave12_cost{$href->{item}} +; $href->{percent_profit} = sprintf "%.1f", ($href->{profit} / $href +->{unit_price}) * 100; . . . . # add to an array for output to Excel # (Data added in order of '@col' array) push @data, [ @$href{ @col } ]; } $sth_3->finish(); my @transposed = transpose( @data ); my $file = 'sales.csv'; open my $out, ">", $file or die "Unable to create $file. $!"; # print to CSV file for my $row (@transposed) { print $out join(",", @$row), "\n"; } close $out or die "Unable to close $file. $!"; sub transpose { my (@data, @trans) = @_; for my $r (0 .. $#data) { for my $c (0 .. $#{ $data[$r] }) { $trans[$c][$r] = $data[$r][$c]; } } return @trans; }

Just out of curiosity, why are you rewriting the code? Does it still work, (even if it's not the best/most pretty)?

If there are embedded commas in any of the fields, the print to the file will fail. The statement print $out join(",", @$row), "\n"; would have to be replaced by a method from a module like Text::CSV_XS.


In reply to Re^5: Linking and Combining Two Arrays by Cristoforo
in thread Linking and Combining Two Arrays by roguez33

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.