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

The first table select is for calculated 12 month average cost of parts.

Output two fields $c_1_ITEM and $c_9_12M_UNIT_COST

The second is multiple table select for parts date based on current month data.

Output many fields. $D_item, $D_agc, and etc....

Parts here being "item". array 1 item "$c_1_ITEM" = array 2 item "$D_item". Add average cost "$c_9_12M_UNIT_COST" to table 2 based on item.

Array two after - $D_item, $D_agc, c_9_12M_UNIT_COST, and etc.....

There is not a one to one relationship here, so a push would not work. But every part "item" in table two has a corsponding entry in table one.

I understand that I probably need to do a hash of hash, but I am not sure how to get there. I suspect a foreach is needed also.

Thanks!

Replies are listed 'Best First'.
Re^3: Linking and Combining Two Arrays
by Cristoforo (Curate) on Feb 03, 2010 at 01:28 UTC
    You say that you want to add the average cost, which is located in the first table, to the results from table 2. And that for each item in the second table, there is an item in the first table that matches.

    The way you set up the arrays is very uncommon and may not be what you want. The data structure you choose depends in part in what you want to do with the results. Just print them out to a report, perform calculations on them, etc. By looking at the fields you're capturing, these seem to be business statistics. It would be helpful to know what it is you want to do with the results. If each of the variables you set up equaled the number of returned fields, there are about 21 of them.

      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.

        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.