简体   繁体   English

如何将从数据库中检索到的数据写到2D数组中,以及如何在Perl中将其取回?

[英]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 . 我在下面尝试将数据写入二维数组(array_source),但最终在单维数组(array_source)中使用。下面是代码段,请仔细阅读并让我知道将其写入2D数组的方法。

$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 ? 一旦将数据检索到array_source中,如何将其与另一个2D数组进行比较并列出匹配集?

Example : 范例:

Array 1 - Source Array [100 5100, 101 5100, 102 6000, 104 7879, 444 287299, 771 111] 阵列1-源阵列[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] 阵列2-应该与来源[100 5100、101 5200、102 0、772 800、104 7879]进行比较

Array 3 - This should be the output - Singe dimensional [100, 104] 数组3-这应该是输出-单维[100,104]

Please spare with alignment for above Arrays and consider 1 & 2 as two dimensional and 3 as single dimensional. 请保留上面的数组的对齐方式,并将1和2视为二维,将3视为一维。

push (@array_source, @array[0,1]); should become push (@array_source, [@array[0,1]]); 应该成为push (@array_source, [@array[0,1]]); - you want to push a new array onto the top-level array. -您想将一个新数组推送到顶层数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM