arraysperlmultidimensional-arrayperlscript

How to write data retrieved from DB into 2D array and how can this be retrieved back in Perl?


I tried below for writing data into two dimensional array(array_source) but ended up in single dimensional array(array_source).. Below is code snippet, please review and let me know the ways to write it into 2D array so that it can be .

$DBHd = DBI->connect( "dbi:Oracle:host=$host;port=$port;sid=$SID", $user_name, $password);
$DBSth = $DBHd->prepare("SELECT EMP_ID,sal FROM emp");
$DBSth->execute();
my @array_temp;
my @array_source; # Should be 2D array and it should contain both values
my @array_source_in; # Should contain only employee IDS alone

while (my @array= $DBSth->fetchrow_array())
{
    push (@array_source, @array[0,1]);
    push (@array_source_in, @array[0]);
};

 print "Data in source : @array_source";
   print "\n";
 print "Data in input : @array_source_in";

Once data is retrieved into array_source how can this be compared with another 2D array and list the matching sets ?

Example :

Array 1 - Source Array [100 5100, 101 5100, 102 6000, 104 7879, 444 287299, 771 111]

Array 2 - Should be compared against source [100 5100, 101 5200, 102 0, 772 800, 104 7879]

Array 3 - This should be the output - Singe dimensional [100, 104]

Please spare with alignment for above Arrays and consider 1 & 2 as two dimensional and 3 as single dimensional.


Solution

  • push (@array_source, @array[0,1]); should become push (@array_source, [@array[0,1]]); - you want to push a new array onto the top-level array.