in reply to Re^3: Linking and Combining Two Arrays
in thread Linking and Combining Two Arrays

I am probably making this more difficult then nescessary. I have been leveraging code done by a previous programmer, as I am very new to PERL.

I welcome better ideas on how to get the desired output, which is an final excel CSV.

The avgerage cost is to be used to preform calulations in the second "array" .

I suspect a foreach loop next to pull in average cost and caluculate new fields. Caluclate a couple of other fields based on available data and then output to CSV.

Replies are listed 'Best First'.
Re^5: Linking and Combining Two Arrays
by Cristoforo (Curate) on Feb 05, 2010 at 17:28 UTC
    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.

      Thanks, I will give that a try!!

      The code is gone. We checked many machines over the network. The CSV report is magicaly generated. No one knows how. They want to change the report, so it is up to me to recreate the report.

      Thanks again!