in reply to Multidimentional array help

Others have already told you the "know-how." Here is the "know-what" --

Multi-dimensional data structures are constructed using references (go look up references in Perl docs). An array (or a hash, for that matter, which is nothing but a special kind of array) can only contain scalars, and references are always scalars. So, you created references to arrays, and stick them in the parent array. Thus you achieve an array_of_arrays (aoh) or an array_of_hashes (aoh) or any variant thereof such as hoa, hoh, and so on.

So, how do you visualize this?

Well, look at your data .. A060,US,M10,WEDNESDAY,SEASONAL A061,US,M10,WEDNESDAY,SEASONAL A062,US,M10,WEDNESDAY,SEASONAL A063,US,M10,WEDNESDAY,SEASONAL A064,US,M10,WEDNESDAY,SEASONAL .. .. Put each line in a square bracket making it into an array reference .. [A060,US,M10,WEDNESDAY,SEASONAL] [A061,US,M10,WEDNESDAY,SEASONAL] [A062,US,M10,WEDNESDAY,SEASONAL] [A063,US,M10,WEDNESDAY,SEASONAL] [A064,US,M10,WEDNESDAY,SEASONAL] .. .. Now, separate each line with a comma, and put the entire block inside parens making it an array .. my @aoa = ( [A060,US,M10,WEDNESDAY,SEASONAL], [A061,US,M10,WEDNESDAY,SEASONAL], [A062,US,M10,WEDNESDAY,SEASONAL], [A063,US,M10,WEDNESDAY,SEASONAL], [A064,US,M10,WEDNESDAY,SEASONAL], );

done.

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re^2: Multidimentional array help
by sasrs99 (Acolyte) on May 31, 2007 at 18:14 UTC
    I've been hammering away at this for a while and I'm stuck. Is there a way to simulate the actual execute by simply printing the string for each element in @dlrloc_array that would be run by the execute?
    my @dlrloc_array = qw(A100 A200 A300 A400 A500 A600 A700 A800); my $dlrgrp_name = 'RUSH'; my $dlrdiv = 'PB'; my $dlrcountry = 'USA'; my $runDOW = 'WEDNESDAY'; my $daysofsupply = 45; my $ordertype = 'SEASONAL'; my $plannercode = 'M63'; my $size = @dlrloc_array; if ( $size > 0 ) { my $table = 'pac.promo_rpt'; my @fields = qw( dlrcode dlrgroup division country planner ordertype daysofsupply run_dow sof_or_excel abc_co +des ); my $fields = join(', ', @fields); my $places = join(', ', ('?') x @fields); my $sql = "INSERT into $table ($fields) values ($places)"; for my $a_val ( @dlrloc_array ) { $sth->execute( $a_val, $ordtype ); } }
      Now you have a question that is totally different from the original question. What you are asking for now are known as "bind variables." You need to do something like so (and, your code above is highly incomplete -- I will assume that you have better, more complete code in real life) --

      my $sth = $dbh->prepare(qq{ INSERT into pac.promo_rpt ( dlrcode, dlrgroup, division, country, planner, ordertype, daysofsupply, run_dow, sof_or_excel, abc_codes ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) }); $sth->execute( $foo, $bar, $baz, $qux... );

      The number of bind variables in the execute step have to be the same and in the same order as represented by the ? in the prepared statement. Look up the docs on bind variables in DBI.pm.

      --

      when small people start casting long shadows, it is time to go to bed