Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Table building confusion Help Wanted

by basicdez (Pilgrim)
on Jan 16, 2002 at 02:52 UTC ( #139073=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to build a table to reference the following informaiton in a table like retrieve. Please help me (if you can)

Entity_ID | Customer_number | SSN |Relationship A1 | 007 | 999-99-9999 | Self C1 | 008 | 888-88-8888 | Spouse S1 | 005 | 777-77-7777 | Son
Here is the code that I am trying to use. In a condensed version of course....
#!/usr/bin/perl-Tw use strict; 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 $count = 3; my @PER_Data; my @major_PER_Data; my $j = 0; for ( 0 .. $count - 1 ) { { @PER_Data = ($dataA[$j], $dataB[$j], $dataC[$j], $dataD[$j]); $j++; } push @major_PER_Data, [@PER_Data]; } my @PER_Item; my @PER_Entity; my @PER_Customer_ID; my @PER_SSN; my @PER_Relationship; my $Num_OF_PER = 0; my $i = 0; foreach my $PER_item (@major_PER_Data) { foreach my $PER_subitem (@{$PER_item}) { $PER_Item[$i] = $PER_subitem; $i++ } $PER_Entity[$Num_OF_PER] = $PER_Item[0]; $PER_Customer_ID[$Num_OF_PER] = $PER_Item[1]; $PER_SSN[$Num_OF_PER] = $PER_Item[2]; $PER_Relationship[$Num_OF_PER] = $PER_Item[3]; print "$PER_Entity[$Num_OF_PER] \n"; print "$PER_Customer_ID[$Num_OF_PER] \n"; print "$PER_SSN[$Num_OF_PER] \n"; print "$PER_Relationship[$Num_OF_PER] \n"; $Num_OF_PER++; }

Problem #1

Why when I am expecting this to send me back the table, is it only sending back the following...
A1 007 999-99-9999 self A1 007 999-99-9999 self A1 007 999-99-9999 self

Problem #2 - If problem #1 can be solved

If we can find the answer to this question, then I can go onto the next thing which is rather than printing out the correct results, how do I put them into a table for referencing purposes?

I would take any advice good or bad that you may have to offer on this one

peace, LOVE and ((code))


Replies are listed 'Best First'.
(jeffa) Re: Table building confusion Help Wanted
by jeffa (Bishop) on Jan 16, 2002 at 03:17 UTC
    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.


    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.

      Do I have to install Data::Dumper or is this already a part of perl>

      peace, LOVE and ((code))

Re: Table building confusion Help Wanted
by jwest (Friar) on Jan 16, 2002 at 03:39 UTC
    My initial reaction is to ask why you're doing it this way? Arrays and list indexes are not your friend, usually. Hashes, on the other hand, are:
    use strict; use warnings; my %entity = ( A1 => { customerID => '007', ssn => '999-99-9999', relationship => 'self', }, B1 => { customerID => '008', ssn => '888-88-8888', relationship => 'spouse', }, C1 => { customerID => '009', ssn => '777-77-7777', relationship => 'son', }, ); foreach my $e (keys(%entity)) { print "$e\n"; print "$entity{$e}{'customerID'}\n"; print "$entity{$e}{'ssn'}\n"; print "$entity{$e}{'relationship'}\n"; }

    And for that matter, this code could be improved, but you're starting to get the idea- you might want to change the primary key away from entity to customerID, for example, but it still largely holds. You're doing a lot of data thrashing, but not what I assume your intended goals are.

    If you want to continue using arrays, I definitely suggest you follow jeffa's approach, then.

    As far as putting them into a table for referencing purposes, depending on what you mean, I may have just done that for you. If you're looking to put them in a database, you should check out DBI.

    As far as what's causing your problems, I can't tell from the code you presented above. At a glance, it looks like it should work.

    Hope this helps!


    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
Re: Table building confusion Help Wanted
by PrakashK (Pilgrim) on Jan 16, 2002 at 03:59 UTC
    To address your question #1 first: You forgot to reset $i to 0 before moving on to the next record. Add:
    $i = 0;
    at the end of the loop where you are reading from the array and printing the values out.

    Having said that, I'd use array of hashes instead of array of arrays to store such data. Hashes lend themselves very nicely for storing records.

    #!/usr/bin/perl-Tw use strict; 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 $count = 3; my @major_PER_Data; # save it for (0 .. $count - 1) { push @major_PER_Data, { ID => $dataA[$_], CUST => $dataB[$_], SSN => $dataC[$_], RELATION => $dataD[$_], }; } # print it for my $PER_item (@major_PER_Data) { print "$PER_item->{ID}\n"; print "$PER_item->{CUST}\n"; print "$PER_item->{SSN}\n"; print "$PER_item->{RELATION}\n"; }
    Much smaller and clearer.


Re: Table building confusion Help Wanted
by indapa (Monk) on Jan 16, 2002 at 03:54 UTC
    Using the array approach, if the data you want in your table is being read from a file you could try the following approach:
    #!/apps/bin/perl -w use Data::Dumper; $table = (); while (<>) { chomp; $curr_rec = [split /\|/, $_]; push (@$table, $curr_rec); } print Dumper (\$table);

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://139073]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2022-01-21 11:42 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.