如何在delphi中使用isset()

时间:2012-03-08 08:20:29

标签: 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) {
        $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']++;
        }
    }

1 个答案:

答案 0 :(得分:0)

根据documentation,PHP数组是一种适合所有人的数据结构,可用作有序列表或哈希映射(字典)。 Delphi没有类似的内置数据结构,只能获得向量(有序列表)行为或哈希映射/字典行为。字典行为只能在Delphi 2009+上轻松访问,因为这是引入Generics的版本。

Delphi 2007上提供的易于使用的数据结构可用于inset类型的操作,TStringListSorted := 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;

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

相关问题