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

Sorry, I am a PERL newb. I am trying real hard to get this, but to no avail.

I have two multidemensional arrays created from querying Oracle.

I hope this makes sense.

How do I link the two arrays based on @avg12_cost_array.@avg12_cost_c_1_item equals @book_det_array.@book_det_D_item

to add @avg12_cost_array.@avg12_cost_12M_UNIT_COST to @book_det_array

1st array:

while (@row = $sth_2->fetchrow_array) { $c_1_ITEM =~ s/\s*$//; if ($c_9_12M_UNIT_COST =~ /\./) { $c_9_12M_UNIT_COST = sprintf("%.2f", $c_9_12M_UNIT_COST); } push(@avg12_cost_c_1_item, $c_1_ITEM); push(@avg12_cost_12M_UNIT_COST, $c_9_12M_UNIT_COST); # print "$c_1_ITEM, $c_9_12M_UNIT_COST\n"; } $sth_2->finish(); @avg12_cost_array = ( [@avg12_cost_c_1_item], [@avg12_cost_12M_UNIT_CO +ST], ); second array: $sth_3->fetchrow_array; while (@row = $sth_3->fetchrow_array) { # remove trailing spaces $D_agc =~ s/\s*$//; $D_market =~ s/\s*$//; $D_analysis_code =~ s/\s*$//; $D_item =~ s/\s*$//; $D_customer =~ s/\s*$//; $D_key_val2 =~ s/\s*$//; $D_key_val3 =~ s/\s*$//; $D_terms_code =~ s/\s*$//; $D_audit_seq =~ s/\s*$//; $D_aud_date =~ s/\s*$//; $D_aud_time =~ s/\s*$//; $D_opr_id =~ s/\s*$//; $D_action =~ s/\s*$//; $D_column_name =~ s/\s*$//; $D_new_value =~ s/\s*$//; $D_old_value =~ s/\s*$//; $D_tot_canc_qty =~ s/\s*$//; $D_tot_ord_qty =~ s/\s*$//; $D_tot_ship_qty =~ s/\s*$//; $D_ship_complete =~ s/\s*$//; $D_unit_price =~ s/\s*$//; # define PL field ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ $mkt_1 = substr $D_market, 0, 1; if ($mkt_1 == "1") { $dPl = "A&D"; } else { $dPl = "MCP"; } push(@book_det_dPl, $dPl); push(@book_det_agc, $D_agc); push(@book_det_D_market, $D_market); push(@book_det_D_analysis_code, $D_analysis_code); push(@book_det_cur_per_month, $cur_per_month); push(@book_det_D_item, $D_item); push(@book_det_D_customer, $D_customer); push(@book_det_D_key_val2, $D_key_val2); push(@book_det_D_key_val3, $D_key_val3); push(@book_det_D_terms_code, $D_terms_code); push(@book_det_D_unit_price, $D_unit_price); push(@book_det_D_audit_seq, $D_audit_seq); push(@book_det_D_aud_date, $D_aud_date); push(@book_det_D_aud_time, $D_aud_time); push(@book_det_D_opr_id, $D_opr_id); push(@book_det_D_action, $D_action); } $sth_3->finish(); # Consolidate the information into an array @book_det_array = ( [@book_det_dPl], [@book_det_agc], [@book_det_D_market], [@book_det_D_analysis_code], [@book_det_cur_per_month], [@book_det_D_item], [@book_det_D_customer], [@book_det_D_key_val2], [@book_det_D_key_val3], [@book_det_D_terms_code], [@book_det_D_unit_price], [@book_det_D_audit_seq], [@book_det_D_aud_date], [@book_det_D_aud_time], [@book_det_D_opr_id], [@book_det_D_action] );
Thanks! Patrick

Replies are listed 'Best First'.
Re: Linking and Combining Two Arrays
by merlyn (Sage) on Feb 01, 2010 at 18:39 UTC
    When you have a lot of similar names like this:
    $D_agc =~ s/\s*$//; $D_market =~ s/\s*$//; $D_analysis_code =~ s/\s*$//; $D_item =~ s/\s*$//; $D_customer =~ s/\s*$//; $D_key_val2 =~ s/\s*$//; $D_key_val3 =~ s/\s*$//; $D_terms_code =~ s/\s*$//; $D_audit_seq =~ s/\s*$//; $D_aud_date =~ s/\s*$//; $D_aud_time =~ s/\s*$//; $D_opr_id =~ s/\s*$//; $D_action =~ s/\s*$//; $D_column_name =~ s/\s*$//; $D_new_value =~ s/\s*$//; $D_old_value =~ s/\s*$//; $D_tot_canc_qty =~ s/\s*$//; $D_tot_ord_qty =~ s/\s*$//; $D_tot_ship_qty =~ s/\s*$//; $D_ship_complete =~ s/\s*$//; $D_unit_price =~ s/\s*$//;
    It's a clue that you have a data structure, not a bunch of unrelated scalars. Consider the use of a proper hash to hold these items.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      You are correct they are data structures!

      The key values are "item" ( $c_1_ITEM and $D_item) in each array.

      Can the be more than one value per key in a hash array?

        Can the be more than one value per key in a hash array?
        Yes, in HASHES OF ARRAYS
Re: Linking and Combining Two Arrays
by Cristoforo (Curate) on Feb 01, 2010 at 22:39 UTC
    I am having trouble seeing what you want to do. 'Linking' the arrays doesn't tell me what is you want to achieve. Maybe you could elaborate further on your basic problem.

    Could you show us your SELECT statements and maybe I could see how you want to join the results?

      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!
        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.