csv Array Problem

  • Hallo,

    habe erst heute wieder Internet (war 1 Woche von der Welt abgeschnitten)
    ich hoffe Ihr hattet alle schöne Feiertage.

    Ich habe eine csv Funktion gefunden
    die ich toll finde. Leider habe ich noch Probleme mit Arrays!
    Kann mir jemand sagen wie ich mit einem Loop
    die Felder lesen kann?

    So sieht die csv aus:

    [autoit]


    Hund;Alter;Pass
    Collie;1 Jahr; geimpft
    Dackel;Welpe;geimpft

    [/autoit]

    das ist die Funktion:

    [autoit]


    #include <array.au3>
    Main()

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

    Func Main()

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

    Local $sAdr = "csv.txt"
    Local $h = FileOpen($sAdr, 0)

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

    Local $aRet = _CSV2Array($h, Default, True, 1)

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

    FileClose($h)
    _ArrayDisplay($aRet)

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

    EndFunc ;==>Main

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

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

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

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

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

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

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

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

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

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

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

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

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

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

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

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

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]


    Viele Grüße
    Ilse ;)

    Einmal editiert, zuletzt von Ilse (30. Dezember 2010 um 14:54)

    • Offizieller Beitrag

    Hallo,

    das geht mit Hilfe von Ubound!

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    Main()

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

    Func Main()

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

    Local $sAdr = "csv.txt"
    Local $h = FileOpen($sAdr, 0)

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

    Local $aRet = _CSV2Array($h, Default, True, 1)

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

    FileClose($h)
    ;~ _ArrayDisplay($aRet)

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

    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    For $iCol = 0 To UBound($aRet, 2) - 1 ; Spalten von $aRet
    ConsoleWrite($aRet[$iRow][$iCol] & @TAB & @TAB)
    Next
    ConsoleWrite(@CRLF)
    Next

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

    EndFunc ;==>Main

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

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

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

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

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

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

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

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

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

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

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

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

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

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

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

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

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]
  • Hallo Bernd,

    danke Dir.
    Kannst du mir bitte noch etwas helfen:

    Frage 1:

    Wie kann ich eine bestimmte Zelle lesen?
    Ich meine daß im Loop z.B. nur die erste Zelle also: Dackel, Collie... nacheinander gezeigt wird!

    Frage 2:

    Wie kann man den kpl. Datensatz anzeigen?
    z.B. Dackel, 1Jahr, geimpft
    das klappt leider nicht, es kommt alles einzeln.

    [autoit]


    #include <array.au3>
    Main()

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

    Func Main()

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

    Local $sAdr = "csv.txt"
    Local $h = FileOpen($sAdr, 0)

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

    Local $aRet = _CSV2Array($h, Default, True, 1)

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

    FileClose($h)
    ;~ _ArrayDisplay($aRet)

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

    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    For $iCol = 0 To UBound($aRet, 2) - 1 ; Spalten von $aRet
    ConsoleWrite($aRet[$iRow][$iCol] & @TAB & @TAB)
    Msgbox(4,"",$aRet[$iRow][$iCol]) ; hier habe ich die Werte eingebaut
    Next
    ConsoleWrite(@CRLF)
    Next

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

    EndFunc ;==>Main

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

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

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

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

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

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

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

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

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

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

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

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

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

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

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

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

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]


    Grüße
    Ilse ;)

    • Offizieller Beitrag

    Hallo,

    dafür musst du die Daten wieder so zusammenstellen wie du sie brauchst!

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    Main()

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

    Func Main()

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

    Local $sAdr = "csv.txt"
    Local $h = FileOpen($sAdr, 0)

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

    Local $aRet = _CSV2Array($h, Default, True, 1)

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

    FileClose($h)
    ;~ _ArrayDisplay($aRet)

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

    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    $szZeile = ""
    For $iCol = 0 To UBound($aRet, 2) - 1 ; Spalten von $aRet
    $szZeile &= $aRet[$iRow][$iCol] & @TAB
    Next
    Msgbox(4,"Zeile",$szZeile) ; hier habe ich die Werte eingebaut
    Next

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

    For $iCol = 0 To UBound($aRet, 2) - 1 ; Spalten von $aRet
    $szSpalte = ""
    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    $szSpalte &= $aRet[$iRow][$iCol] & @CRLF
    Next
    Msgbox(4,"Spalte",$szSpalte) ; hier habe ich die Werte eingebaut
    Next
    EndFunc ;==>Main

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

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

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

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

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

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

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

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

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

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

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

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

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

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

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

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

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]
  • Hallo Bernd,

    muß mich leider nochmal melden. Bin die ganze Zeit am probieren.
    Ich verstehe das mit den Arrays noch nicht so ganz.

    Ich kann z.Zt. nur Spalten und Zeilen lesen aber keine einzelne "ZELLEN!"

    Hier erhalte ich Fehler

    [autoit]


    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    $szZeile = ""
    $Alter = "" ; die Zelle Alter möchte ich lesen!
    For $iCol = 0 To UBound($aRet, 2) - 1 ; Spalten von $aRet
    ;$szZeile &= $aRet[$iRow][$iCol] & @TAB
    $Alter &= $aRet[$iCol] & @TAB ; hier komme ich nicht weiter!
    Next
    ;Msgbox(4,"Alter",$Alter) ; hier habe ich die Werte eingebaut
    Next

    [/autoit]

    Grüße
    Ilse ;)

    • Offizieller Beitrag

    Hallo,

    eine Zelle im 2D-Array wird immer durch eine Zeile- und Spaltenposition bestimmt!

    Spoiler anzeigen
    [autoit]

    $iCol = 1 ; Hund = 0; Alter = 1; Pass = 2
    For $iRow = 0 To UBound($aRet, 1) - 1 ; Zeilen von $aRet
    MsgBox(0, "Zeile: " & $iRow & " Spalte: " & $iCol, $aRet[$iRow][$iCol])
    Next

    [/autoit]
  • Hallo Bernd,

    merci für deine Hilfe und den Link 2D-Array.
    Das würde ich gerne lernen.
    Hoffentlich kapiere ich das ;)

    Ich wünsche dir schon jetzt einen guten Rutsch
    ins neue Jahr 2011

    Liebe Grüße
    Ilse ;)

    • Offizieller Beitrag

    Das wünsche ich dir auch!

    Das mit den Array's sieht komplizierter aus als es ist, dass lernst du schon! ;)

  • Hallo Ilse,

    hier einmal dein Skript mit zum Verständnis eingebautem Hilfebeipiel zu UBound:

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    Main()
    Func Main()

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

    Local $sAdr = @ScriptDir & "\new.txt"
    Local $h = FileOpen($sAdr, 0)

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

    Local $aRet = _CSV2Array($h, "Auto", True, 1)
    FileClose($h)
    _ArrayDisplay($aRet)
    $rows = UBound($aRet)
    $cols = UBound($aRet, 2)
    $dims = UBound($aRet, 0)

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

    MsgBox(0, "Das " & $dims & "-dimensionale Array hat", _
    $rows & " Zeilen, " & $cols & " Spalten")

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

    ; Zeigt den Inhalt von $aRet
    $output = ""
    For $r = 0 To UBound($aRet, 1) - 1
    $output = $output & @LF
    For $c = 0 To UBound($aRet, 2) - 1
    $output = $output & $aRet[$r][$c] & " "
    Next
    Next
    MsgBox(4096, "Inhalt des Arrays", $output)

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

    MsgBox(0, "Zeile 2/Spalte 1:", $aRet[2][1])
    ;Ausgabe von Alter des Hundes in Zeile 2 /Spalte 1
    ;Achtun Array ist NULL-basierend
    EndFunc ;==>Main

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

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

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

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

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

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

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

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

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

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

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

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

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

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

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

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

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]

    Hier noch ein Bild, das zeigt wie die letzte MsgBox zustandekommt:
    autoit.de/wcf/attachment/12214/

    mfg autoBert

  • Hallo autobert,

    dank dir für deine Infos
    Vielleicht werde ich nächstes Jahr um diese
    Zeit über Arrays "schmunzeln"

    ...auch dir einen guten Rutsch ins Jahr 2011

    Liebe Grüße
    Ilse ;)