Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Table Manipulation

by BillKSmith (Vicar)
on Aug 26, 2012 at 02:57 UTC ( #989775=note: print w/replies, xml ) Need Help??

in reply to Table Manipulation

Array of arrays works well. Build a line of table_3 (using array slices) when a match is found.

use strict; use warnings; use Data::Dumper; my @table_1 = ( # A B C [ 1, 1, 1 ], [ 1, 0, 2 ], [ 0, 1, 3 ], ); my @table_2 = ( # A P Q R [ 1, 1, 0, 1 ], [ 1, 0, 1, 0 ], [ 0, 0, 0, 1,], ); my @table_3; foreach my $row_of_1 (@table_1) { foreach my $row_of_2 (@table_2) { if ( $row_of_1->[0] == $row_of_2->[0] ) { push @table_3, [@{$row_of_1}[1..2],@{$row_of_2}[1..3]]; } } } print Dumper \@table_3;

Replies are listed 'Best First'.
Re^2: Table Manipulation
by aartist (Scribe) on Aug 27, 2012 at 15:26 UTC
    Helpful solution. Thanks.
    I have 2 more requirement. I need the column names in the answer. 2. I have more table_2 types tables for specified columns of in table_1, that I need to merge. For example I have table_2 that also start with 'B' or 'C' in addition to what is already there (table_2 that starts with 'A').

      Try to do the second new requirement yourself. Make a second program exactly the same as the first except for the subscripts. We can look into generalizing later.

      Do you just need column names in the output or do you need a data structure which includes the names?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989775]
[marto]: it's a shame Solaris is such a PITA to build on :)
LanX great movie!
[Discipulus]: ah i become monsignor the same day of sysadmin day
[marto]: not so great OS :P
Eily never heard of the movie "LanX"
[Eily]: the LanX OS either now that I think of it :P
[hippo]: Congratulations, Discipulus!
[Discipulus]: Satura Lanx was a latin genre of show
[Discipulus]: coming from the meaning of 'mixed dish'

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-07-28 09:57 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (426 votes). Check out past polls.