sasrs99 has asked for the wisdom of the Perl Monks concerning the following question:
I don't know enough about Perl arrays to understand how to do this so I would appreciate any advice.
I have an array defined as such:
@dlr_loc = (A060, A061, A062, A063, A064);
I basically want to create another array has these values in the 1st column, and I want to add other values (columns) after each of them like this:
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
The other columns are populated with constants. The idea here is that I am trying to build this array so I can then spin through the array and insert each of these rows into an Oracle table.
Re: Multidimentional array help
by GrandFather (Saint) on May 29, 2007 at 22:37 UTC
|
use strict;
use warnings;
my @dlr_loc = qw(A060 A061 A062 A063 A064);
my @aoa = map {[$_, qw(US M10 WEDNESDAY SEASONAL)]} @dlr_loc;
print "@$_\n" for @aoa;
Prints:
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
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
|
Thanks for the advice. One last thing, if I have a variable called $ordtype with a value = 'SEASONAL', how can I substitute this variable so I don't have to hard-code the string?
| [reply] |
|
...
my $ordtype = 'SEASONAL';
my @aoa = map {[$_, qw(US M10 WEDNESDAY), $ordtype]} @dlr_loc;
...
DWIM is Perl's answer to Gödel
| [reply] [d/l] |
Re: Multidimentional array help
by FunkyMonk (Chancellor) on May 29, 2007 at 22:54 UTC
|
Grandfather's answer works well, but if you're new to Perl, you might find this more understandable
my @another_array;
for ( @dir_loc )
{
push @another_array, [ $_, qw/US M10 WEDNESDAY SEASONAL/ ]
}
You can access this new array using something like:
print $another_array[2][3];
#WEDNESDAY
| [reply] [d/l] [select] |
Re: Multidimentional array help
by blazar (Canon) on May 29, 2007 at 22:46 UTC
|
Multidimentional array help
Perl (5) doesn't really have "multidimensional arrays" though, with references you can come close and GrandFather told you how to do it. Now just pay attention:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @x=qw/x y z/;
my @y=@x;
$y[1]='u';
print Dumper \@x, \@y;
@x=( map [qw/x y z/], 1..3 );
@y=@x;
$y[1][1]='u';
print Dumper \@x, \@y;
__END__
And then look at the output:
| [reply] [d/l] [select] |
Re: Multidimentional array help
by graff (Chancellor) on May 30, 2007 at 01:46 UTC
|
The idea here is that I am trying to build this array so I can then spin through the array and insert each of these rows into an Oracle table.
If your @dlr_loc array is really small, as you seem to suggest, you can supplement the array within the sql statement, if that seems appropriate:
my $ordtype = 'SEASONAL';
# ...
my $sql = "insert into that_table (a_col, b_col, c_col, d_col, e_col)
+values (?,'US','M10','WEDNESDAY',?)";
my $sth = $dbh->prepare( $sql );
for my $a_val ( @dlr_loc ) {
$sth->execute( $a_val, $ordtype );
}
Of course, if other fields turn out to be variables (and if you actually end up with a nested loop structure), that's fine -- just use more "?" placeholders in the sql statement.
OTOH, if you are going to be inserting a lot of rows (e.g. tens of thousands or more), your best bet is to use your perl script to create a tab-delimited text file containing the field values row-by-row (line-by-line), then invoke "sqlload" (or "sqlloader"?) -- the native Oracle data-import utility -- to actually load the contents of the file into the database. That will go a lot faster than using Perl/DBI to execute a long series of "insert into ..." statements (and the error-handling done by sqlload(er) will generally much better than you'd want to do yourself in Perl). | [reply] [d/l] |
Re: Multidimentional array help
by punkish (Priest) on May 30, 2007 at 04:14 UTC
|
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
| [reply] [d/l] |
|
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 );
}
}
| [reply] [d/l] |
|
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
| [reply] [d/l] |
|
|