VBS,从父子结构创建完整的层次结构字符串,递归

时间:2021-04-13 12:42:42

标签: vbscript parent-child

我正在尝试在 Visual Basic 脚本中执行以下操作。有一个元素层次结构,我有最低的元素(基础),我需要创建完整的层次结构 string 。我有一个函数,它返回一个元素的父元素。这意味着我可以要求父母,然后是父母的父母,等等。

示例:B 是 A 的父级,C1、C2 是 B 的父级,...

example image

在这个例子中,结果应该是 3 个字符串的数组(第 1、2、3 行),每个字符串都包含基本元素 A 的完整层次结构。

我试过 for 循环,但不知何故我不能把它放在一起。我不是 VBS 专家,数组处理对我来说看起来很笨拙。有什么提示吗?

编辑:

这是我目前的功能:

Function GetAllParents (strServer, strDimension, strElem)
    
    dim strPath     
    dim strElement
    dim strParent
    dim lngCount 
    dim i
    dim j
    dim k
    dim result
    result = strElem
    strElement = strElem
    lngCount = AleaElement.ElementParentsCount (strServer, strDimension, strElement)
    
    if (lngCount > 0) then
        for i = 1 to lngCount       
            strParent = AleaElement.ElementParentsName(strServer, strDimension, strElement, i)
            result = result & "  " & GetAllParents(strServer, strDimension, strParent) 
        next 
    end if
    GetAllParents = result

End Function

这是我得到的结果:

CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_RET_SAS_RD1 CST_RET_SAS_MNGMT CST_RETAIL_DIVN CST_TOTAL
CST_TOTAL_I&E 
CST_SS_FAE_CUR CST_SS_FAE CST_RET_SAS CST_RET_SAR CST_RETAIL_DIVN CST_TOTAL 
CST_TOTAL_I&E 
CST_RET_SAS_EX_VIR 
CST_RETAIL_ALL CST_RETAIL CST_ALL_COST

这是我需要的结果:

CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_RET_SAS_RD1 CST_RET_SAS_MNGMT CST_RETAIL_DIVN CST_TOTAL
CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_RET_SAS_RD1 CST_RET_SAS_MNGMT CST_RETAIL_DIVN CST_TOTAL_I&E
CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_SS_FAE_CUR CST_SS_FAE CST_RET_SAS CST_RET_SAR CST_RETAIL_DIVN CST_TOTAL 
CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_SS_FAE_CUR CST_SS_FAE CST_RET_SAS CST_RET_SAR CST_RETAIL_DIVN CST_TOTAL_I&E 
CST_700P36 CST_AM_FAE_1002 CST_RM_FAE_NRTH1 CST_OM_FAE_NRTH CST_SS_FAE_CUR CST_RET_SAS_EX_VIR 
CST_700P36 CST_RETAIL_ALL CST_RETAIL CST_ALL_COST

看起来曾经包含的部分不会再次放入。

0 个答案:

没有答案
相关问题