如何在delphi中使用isset()
我正在寻找从PHP代码到Delphi的转换。目前,我在处理PHP代码中的Isset函数时遇到了麻烦。有没有办法把下面的代码转换成Delphi如何在delphi中使用isset(),delphi,delphi-2007,Delphi,Delphi 2007,我正在寻找从PHP代码到Delphi的转换。目前,我在处理PHP代码中的Isset函数时遇到了麻烦。有没有办法把下面的代码转换成Delphi $collection = array( 'doc1' =>'php powerbuilder', 'doc2' =>'php visual'); $dictionary = array(); $docCount = array(); foreach ($collection as $docID => $doc) {
$collection = array(
'doc1' =>'php powerbuilder',
'doc2' =>'php visual');
$dictionary = array();
$docCount = array();
foreach ($collection as $docID => $doc) {
$doc = strtolower($doc);
$terms = explode(' ', $doc);
$docCount[$docID] = count($terms);
foreach ($terms as $term) {
if (!isset($dictionary[$term])) {
$dictionary[$term] = array('df' => 0, 'postings' => array());
}
if (!isset($dictionary[$term]['postings'][$docID])) {
$dictionary[$term]['df']++;
$dictionary[$term]['postings'][$docID] = array('tf' => 0);
}
$dictionary[$term]['postings'][$docID]['tf']++;
}
}
根据,PHP数组是一种一刀切的数据结构,用作有序列表或哈希映射字典。Delphi没有类似的内置数据结构,您只能获得向量有序列表行为或哈希映射/字典行为。字典行为只有在Delphi2009+上才容易访问,因为它是引入泛型的版本 Delphi2007上提供的易于使用的数据结构,可用于插入类型的操作,是TStringList,其排序方式为:=True。但这不是真正的字典,它只是一个排序的字符串列表,其中每个字符串都可以有一个与之关联的值。您可以这样使用它:
procedure Test;
var L: TStringList;
begin
L := TStringList.Create;
try
L.Sorted := True; // make the list "Sorted" so lookups are fairly fast
L.AddObject('doc1', SomeData); // SomeData can't be string, it needs to be TObject descendant
L.AddObject('doc2', SomeOtherData);
if L.IndexOf('doc3') = -1 then // this is equivalnt to the `inset` test
begin
// doc3 is not in list, do something with it.
end;
finally L.Free;
end;
end;
这当然不是一个完整的答案,但应该让您开始。我需要合并记录数组来实现吗?看起来您需要使用字典请发布您正在使用的Delphi版本,以便我们知道您是否有泛型。也许有人会抽出时间写一些代码,但我怀疑没有人会在不知道泛型是否可用的情况下冒险。我使用的是Delphi2007企业版。谢谢。你试过什么了吗?因为如果不是的话,看起来您希望我们为您翻译代码,这在这里通常是不受欢迎的。TStringList没有接受2个参数的Add方法。您需要改用L.Add'doc1='+SomeData或L.Values['doc1']:=SomeData,然后使用L.IndexOfName。