Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found