Well, how about iterating over one of the arrays by an index value and pushing that index for each of the arrays to a data structure?

use strict; use Data::Dumper; my @dataA = ("A1", "C1", "S1"); my @dataB = ("007", "008", "005"); my @dataC = ("999-99-9999", "888-88-8888", "777-77-7777"); my @dataD = ("self", "spouse", "son"); my $ref; for my $i (0..$#dataA) { push @$ref,[$dataA[$i],$dataB[$i],$dataC[$i],$dataD[$i]]; } print Dumper $ref;

Not the best way to do it, but it works as long as each array is the same size.

<update>

In answer to your reply below, Data::Dumper comes with the standard Perl distribution. It is already installed, though it is up to you to check for new versions. Since i was careless to not mention this before (confession: i assumed you knew), i will explain a bit more.

When you run this script, you should have the following output:

$VAR1 = [ [ 'A1', '007', '999-99-9999', 'self' ], [ 'C1', '008', '888-88-8888', 'spouse' ], [ 'S1', '005', '777-77-7777', 'son' ] ];

$VAR1 is not the important part here, don't worry about it for now. What is important is the _value_ of $VAR1, an array reference that contains array referneces - AKA a two dimensional array, which can be called a 'table'.

I think this is what you wanted. jwest's solution is possibly better, however. Using his idea, this would be very nice way to do it:

use strict; use Data::Dumper; my @fields = qw(cust_id ssn relation); my @ent = ("A1", "C1", "S1"); my @id = ("007", "008", "005"); my @ssn = ("999-99-9999", "888-88-8888", "777-77-7777"); my @rel = ("self", "spouse", "son"); my %data; for my $i (0..$#ent) { my %hash; @hash{@fields} = ($id[$i],$ssn[$i],$rel[$i]); $data{$ent[$i]} = \%hash; } print Dumper \%data;

Again, i only use Data::Dumper so that when you run this code, you will see what the datastructure you need to later manipulate will look like, which is this:

$VAR1 = { 'C1' => { 'relation' => 'spouse', 'cust_id' => '008', 'ssn' => '888-88-8888' }, 'S1' => { 'relation' => 'son', 'cust_id' => '005', 'ssn' => '777-77-7777' }, 'A1' => { 'relation' => 'self', 'cust_id' => '007', 'ssn' => '999-99-9999' } };

Don't worry about the keys (A1,C1, and S1) being out of order (or even in the order that i presented), you can always use sort to order them for you.

Try this instead of the printing the Dump of %data:

foreach my $key (sort keys %data) { print 'insert into db_table(', join(',',('entity',keys %{$data{$key}})), ') values(', join(',',map { "'$_'" } ($key,values %{$data{$key}})), ")\n"; }
Now, i would not use that code to insert data into one of my database tables (think quoting issues), but i think it really demonstrates just how powerful Perl is at this task.

Ooooo... i just noticed PrakashK's solution. (next day update: i also should be fair and note that even though indapa's solution doesn't use strict (it really doesn't even need to), i like it too.) Why bother saving a key? Using that solution would make my last chunk of code less obfuscated:

for (@major_PER_Data) { print 'insert into db_table(', join(',',keys %$_), ') values(', join(',',map { "'$_'" } values %$_), ")\n"; }

Much nicer, but please name your variables more consisely. I notice a lot of PER prefixes in your code, this is a hint that all variables with that prefix might belong together. This symptom is a good candidate for encapsulation, via an object, or simply an array.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

In reply to (jeffa) Re: Table building confusion Help Wanted by jeffa
in thread Table building confusion Help Wanted by basicdez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.