gpt4 book ai didi

arrays - Perl - 跨模块的数组哈希

转载 作者:行者123 更新时间:2023-12-04 06:19:25 25 4
gpt4 key购买 nike

我对 Perl 有点陌生,我需要一些关于跨模块移动我的 Hash of Arrays 的帮助。

目前我有一个存储数组的数据库模块,如下所示:

    sub getSourceCriteria {

my($self) = shift();
my($sourceId) = shift();
chomp $sourceId;

my(%criteria) =();
$logger->debug("Getting records for Source ID: " . $sourceId);
$dbh=DBI->connect('dbi:ODBC:StkSkrnDB', 'RTETET', 'XXuser01',{ RaiseError => 1, AutoCommit => 0 }) || \
$logger->err_die("Database connection not made: $DBI::errstr\n");

my($sth) = "select a.criteria_id, a.criteria_type, a.criteria_props,a.generalcriteria_id,b.field_id ";
$sth = $sth . "from t_criteria a, t_sourceMapping b where a.generalcriteria_id = (select generalcriteria_id from t_sourcecriteria where source_id =?) ";
$sth = $sth . "and a.criteria_id=b.criteria_id";


my($qry) = $dbh->prepare($sth);
$qry->execute($sourceId) || $logger->error("Could not query for Source Criteria: $DBI::errstr\n");
my(@row)=();
my($tempCount) = 0;

while ( @row = $qry->fetchrow_array ) {
$tempCount = scalar @row;
$logger->debug("Size of retrieved SQL Array : $tempCount");
$criteria{$row[0]} = \@row;
##@{$criteria{$row[0]} } = \@row;


}

return %criteria;
}

我有一个单独的 perl 脚本,它从上面的代码中读取 SQL 输出:
    foreach my $criteria (keys %criterias) { 
@temp = exists( $criterias{$criteria} ) ? @{ $criterias{$criteria} } : ();
##my $tempStr = exists( $criterias{$criteria} ) ? "Yes" : "No";
$arraySize = scalar @temp;
$logger->debug("GENERALCRITERIA_ID is $GENERALCRITERIA_ID and size of array is $arraySize and $temp[0]");
$genCrit_ID = $temp[$GENERALCRITERIA_ID];
$logger->debug("Criteria ID $criteria has Gen Criteria ID $genCrit_ID");
if (0!=$generalCriteria_ID || $generalCriteria_ID != $genCrit_ID ) { ## test for uniqueness
$generalCriteria_ID = -1;
}
else {
$generalCriteria_ID = $genCrit_ID;
}
}# do something with $key and $value
$generalCriteria = $generalCriteria_ID;

}

问题是我一直得到 0 作为检索到的数组大小(第二个片段),即使当我存储数组(在第一个片段中)时,我检查并获得实际的数组大小。

请任何帮助/澄清将不胜感激。

编辑
在 DB 接口(interface)代码中添加了更多代码。

最佳答案

在您的 while循环,您分配给 @row然后存储对该数组的引用。但是,每次循环迭代时,您都在替换 @row 的内容。没有声明一个新的数组。所以最后,你的每一个引用都指向同一件事。

在您的代码中:

my(@row)=();
my($tempCount) = 0;

while ( @row = $qry->fetchrow_array ) {
$tempCount = scalar @row;
$logger->debug("Size of retrieved SQL Array : $tempCount");
$criteria{$row[0]} = \@row;
##@{$criteria{$row[0]} } = \@row;
}

每次 while 循环迭代时,您都会为 @row 分配新值。大批。但自从 my(@row)=();行出现在循环之外, @row数组总是一样的。因此,每次分配给数组时,您都在更改存储在您已经获取的所有引用中的内容。

要解决此问题,您需要为每次迭代声明一个新数组。最简单的方法是将声明移到 while 条件中:
my($tempCount) = 0;

while ( my @row = $qry->fetchrow_array ) {
$tempCount = scalar @row;
$logger->debug("Size of retrieved SQL Array : $tempCount");
$criteria{$row[0]} = \@row;
##@{$criteria{$row[0]} } = \@row;
}

现在每次您引用 \@row您将获得对新数组的引用。

如果您的 $qry->fetchrow_array方法返回一个数组引用,你不会有这个问题:
my $row;
while ($row = $qry->fetchrow_array) {
$logger->debug("Size of retrieved SQL Array : ".@$row);
$criteria{$$row[0]} = $row; # already a reference
}

但我还是会把它写成 while (my $row = ...在我自己的代码中,因为保持范围小是件好事。

关于arrays - Perl - 跨模块的数组哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6781913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com