2012-03-08 46 views
0

我正在寻找从PHP代码到Delphi的转换。目前,我在使用PHP代码中的Isset()函数处理时遇到困难。有什么办法可以将下面的代码转换成Delphi吗?如何在delphi中使用isset()

$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']++; 
     } 
    } 
+0

我是否需要组合记录数组来实现它? – akunyer 2012-03-08 08:23:59

+1

看起来你需要使用字典 – 2012-03-08 08:30:15

+0

请发布你使用的Delphi版本,所以我们知道你是否有泛型。也许有人会找时间写一些代码,但我怀疑任何人都会冒风险,不知道泛型是否可用。 – 2012-03-08 08:41:04

回答

0

PHP阵列,根据documentation,一个尺寸适合所有数据结构,其工作方式的有序列表或作为散列映射(字典)。德尔福没有类似的内置数据结构,你只能得到矢量(有序列表)行为或哈希映射/字典行为。 Delphi 2009+上的字典行为只能轻松访问,因为这是引入泛型的版本。

在Delphi 2007上可用的易于使用的数据结构可用于inset操作类型为TStringList,其模式为Sorted := 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; 

这当然不是一个完整的答案,但应该让你开始。

+0

'TStringList'没有一个带有2个参数的'Add()'方法。您需要使用'L.Add('doc1 ='+ SomeData)'或'L.Values ['doc1']:= SomeData',然后使用'L.IndexOfName()'。 – 2012-03-08 09:53:50