For In Next mit Unterobjekten von Objekten

  • Hallo alle zusammen,

    ich habe folgende Objektstruktur: oHauptobjekt.oUnterobjekt.oUnterUnterobjekt.Attribut
    Diese Struktur wollte ich eigentlich mit

    [autoit]

    For $oUnterobjekt In oHauptobjekt

    [/autoit]


    durchgehen.
    Das hat nicht funktioniert.
    Was habe ich falsch gemacht?

    LG

  • Das Objekt kommt aus einer UDF, die Objekte erzeugt, die wieder Objekte enthalten kann, siehe unten. Die entscheidende Frage ist ja erst einmal: Kann die Variable, der ein Element zugewiesen wird ($oTreeChilds) überhaupt ein Objekt sein?

    Seltsamerweise wird weder 1) noch 2) angezeigt.

    Meine Funktion:

    [autoit]

    $oTree = _FromPathToTree('\\LAREN2\Fachbereichuebergreifend$\', _AttribRules, 2)
    For $oTreeChilds In $oTree ;Durchläuft Childs des Tree
    MsgBox(0, "", "1");;; 1)
    MsgBox(0, "Type", VarGetType($oTreeChilds));;; 2)
    Next

    [/autoit]
    _AttribRules
    [autoit]


    Func _AttribRules($sPath)
    $sAttrib = FileGetAttrib($sPath)
    ;~ If MsgBox(0, $sPath, StringRegExp($sPath, '(?i)\.lnk(?!.)'), 1);;;
    If StringRegExp($sPath, '(?i)\.lnk(?!.)') Then Return True ;Wenn Verknüpfung dann OK
    Return StringRegExp($sAttrib, '[D]') ;Wenn Directory dann OK, sonst nicht
    EndFunc ;==>_AttribRules

    [/autoit]
    _FromPathToTree-Funktion
    [autoit]


    Func _FromPathToTree($sPath, $hRules, $iLevel = -1)
    Local $oTree = _Tree_Object()
    Local $hFile = FileFindFirstFile($sPath & '*')
    Local $sFile = FileFindNextFile($hFile)
    Local $bDir = @extended
    Local $bRules, $sKey, $vTmp, $i = 0

    [/autoit] [autoit][/autoit] [autoit]

    $oTree.append('__obj__') = $sPath ; Fügt Pfad hinzu immer an erster Stelle im Array

    [/autoit] [autoit][/autoit] [autoit]

    If $iLevel Then
    While $sFile
    $bRules = $hRules($sPath & $sFile)

    [/autoit] [autoit][/autoit] [autoit]

    If 1 Then ;$bRules Then
    ;~ $sKey = StringRegExpReplace($sFile, '[^a-zA-Z_]', '')
    $i += 1
    $sKey = $i
    If $bDir Then
    $vTmp = _FromPathToTree($sPath & $sFile & '\', $hRules, $iLevel - 1)
    $oTree.append($sKey) = ((IsObj($vTmp)) ? ($vTmp) : ($sPath & $sFile))
    Else
    $oTree.append($sKey) = $sPath & $sFile
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $sFile = FileFindNextFile($hFile)
    $bDir = @extended
    WEnd
    Else
    FileClose($hFile)
    Return
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    FileClose($hFile)
    Return $oTree
    EndFunc ;==>_FromPathToTree

    [/autoit]
    Tree-UDF
    [autoit]


    ; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

    [/autoit] [autoit][/autoit] [autoit]

    #include-once
    #include <AutoItObject.au3>

    [/autoit] [autoit][/autoit] [autoit]

    _AutoItObject_Startup()
    OnAutoItExitRegister('__Tree_UDF')

    [/autoit] [autoit][/autoit] [autoit]

    ; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

    [/autoit] [autoit][/autoit] [autoit]

    Func _Tree_Object()
    Local $aiArr[1] = [0]

    [/autoit] [autoit][/autoit] [autoit]

    Local $oTree = _AutoItObject_Create()
    ; PUBLIC
    _AutoItObject_AddMethod($oTree, 'append', '__Tree_Append')
    _AutoItObject_AddMethod($oTree, 'exists', '__Tree_Exists')
    _AutoItObject_AddMethod($oTree, 'remove', '__Tree_Remove')
    _AutoItObject_AddMethod($oTree, 'insert', '__Tree_Insert')
    _AutoItObject_AddMethod($oTree, 'copy', '__Tree_Copy')
    ; READONLY
    _AutoItObject_AddProperty($oTree, 'keys', $ELSCOPE_READONLY, $aiArr)

    [/autoit] [autoit][/autoit] [autoit]

    Return $oTree
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_Append($oSelf, $sKey, $vValue = Null)
    Local $avKeys = $oSelf.keys

    [/autoit] [autoit][/autoit] [autoit]

    If $oSelf.exists($sKey) Then
    Return False
    Else
    _AutoItObject_AddProperty($oSelf, $sKey, $ELSCOPE_PUBLIC, $vValue)
    ReDim $avKeys[$avKeys[0] +2]
    $avKeys[0] += 1
    $avKeys[$avKeys[0]] = StringLower($sKey)
    $oSelf.keys = $avKeys
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Return True
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_Exists($oSelf, $sKey)
    Local $avKeys = $oSelf.keys
    Local $i

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $avKeys[0]
    If $avKeys[$i] = $sKey Then Return $i
    Next

    [/autoit] [autoit][/autoit] [autoit]

    Return False
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_Remove($oSelf, $sKey)
    Local $avKeys = $oSelf.keys
    Local $iPos, $i

    [/autoit] [autoit][/autoit] [autoit]

    If $sKey Then
    $iPos = $oSelf.exists($sKey)
    If $iPos Then
    Execute('$oSelf.' & $sKey & ' = Null')
    For $i = $iPos To $avKeys[0] -1
    $avKeys[$i] = $avKeys[$i +1]
    Next
    ReDim $avKeys[$avKeys[0]]
    $avKeys[0] -= 1
    $oSelf.keys = $avKeys
    Else
    Return False
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Return $oSelf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_Insert($oSelf, $sKey)
    Local $oTmp = $oSelf.copy

    [/autoit] [autoit][/autoit] [autoit]

    $oSelf = _Tree_Object()
    $oSelf.append($sKey) = $oTmp

    [/autoit] [autoit][/autoit] [autoit]

    Return $oSelf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_Copy($oSelf)
    Local $oTmp = _AutoItObject_Create($oSelf)

    [/autoit] [autoit][/autoit] [autoit]

    Return $oTmp
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

    [/autoit] [autoit][/autoit] [autoit]

    Func __Tree_UDF()
    _AutoItObject_Shutdown()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

    [/autoit]
  • Dein Beispiel ist immer noch nicht nachvollziehbar.
    Was ist das für ein Objekt?
    Hat das Objekt einen Iterator implementiert (wichtig für For-In-Schleifen)?
    Wie sind die Objekte verschachtelt - By-Reference oder By-Value?

    Aus den Codefetzen die du gepostet hast ist das nicht ersichtlich.
    Vor allem da die Funktionen _AttribRules() und _Tree_Object() fehlen.

    Ich kann deine Frage daher nur allgemein beantworten:
    Ja Objekte kann man verschachteln und mit For-In-Schleifen wieder auslesen:

    [autoit]

    ; Unterdictionary erstellen und füllen
    $dic_Unter = ObjCreate("Scripting.Dictionary")
    $dic_Unter("Wert 1") = "Test 1"
    $dic_Unter("Wert 2") = "Test 2"
    $dic_Unter("Wert 3") = "Test 3"

    [/autoit][autoit][/autoit][autoit]

    ; Oberdictionary erstellen und Unterdictionary hinzufügen:
    $dic_Ober = ObjCreate("Scripting.Dictionary")
    $dic_Ober.Add("Value1", $dic_Unter)

    [/autoit][autoit][/autoit][autoit]

    ; Verschachtelte Objekte auslesen
    For $dic In $dic_Ober.Items
    For $i In $dic.Items
    ConsoleWrite($i & @CRLF)
    Next
    Next

    [/autoit]
  • Sorry, ich habe es nachgetragen.

    In der UDF gibt es die Funktion Tree_Append. Diese regelt das. Wie kann man denn Objekte bei Value verschachteln? Es gibt doch sowieso immer nur eine Referenz zu einem Objekt?

    [autoit]

    Func __Tree_Append($oSelf, $sKey, $vValue = Null)
    Local $avKeys = $oSelf.keys

    [/autoit][autoit][/autoit][autoit]

    If $oSelf.exists($sKey) Then
    Return False
    Else
    _AutoItObject_AddProperty($oSelf, $sKey, $ELSCOPE_PUBLIC, $vValue)
    ReDim $avKeys[$avKeys[0] +2]
    $avKeys[0] += 1
    $avKeys[$avKeys[0]] = StringLower($sKey)
    $oSelf.keys = $avKeys
    EndIf

    [/autoit][autoit][/autoit][autoit]

    Return True
    EndFunc

    [/autoit][autoit][/autoit][autoit][/autoit]