Häufigkeit eines Strings/Integers in Array

  • Hi,

    stehe grade total auf dem Schlauch. Wie kann ich überprüfen wie oft ein String in einem Array steht?

    [autoit]

    $array[0] = "Test"
    $array[1] = "Bla"
    $array[2] = "Test"

    [/autoit]
  • [autoit]

    $array[0] = 3
    $array[1] = "Bla"
    $array[2] = "Test"
    $array[3] = "Test"
    Local $x = 0, $lnum
    For $lnum = 1 to $array[0]
    If $array[$lnum] = "Test" Then $x += 1
    Next
    MsgBox( 0, "", "Der String Test kommt " & $x & "x vor!" )

    [/autoit]
    • Offizieller Beitrag

    Hi,

    schneller Versuch:

    [autoit]

    Dim $array[3]
    $array[0] = "Test"
    $array[1] = "Bla"
    $array[2] = "Test"

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

    MsgBox(0,"",_countStringInArray('Test', $array))

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

    Func _countStringInArray($string, $array, $start = 0, $end = 0)
    Local $count = 0
    If $end = 0 Then $end = UBound($array)-1
    For $i = $start To $end
    If $array[$i] == $string Then $count += 1
    Next
    Return $count
    EndFunc

    [/autoit]

    So long,

    Mega

  • Ja, total einfach, ich war einfach zu verplant. Danke schön. (Muss die Hitze hier sein! :D)

    Hier die nochmal von mir angepasste Funktion

    Spoiler anzeigen
    [autoit]

    Func _ArrayCount($var, ByRef $a_array, $i_case = 0, $i_start = 0, $i_end = 0)
    Local $i_count = 0

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

    If $i_end == 0 Then
    $i_end = UBound($a_array) - 1
    EndIf

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

    If $i_case == 0 Then
    For $i = $i_start To $i_end
    If $a_array[$i] = $var Then
    $i_count += 1
    EndIf
    Next
    ElseIf $i_case == 1 Then
    For $i = $i_start To $i_end
    If $a_array[$i] == $var Then
    $i_count += 1
    EndIf
    Next
    EndIf

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

    Return $i_count
    EndFunc

    [/autoit]
    • Offizieller Beitrag

    Hi,

    hier noch ne Variante, weil ich doch so gern rumspiele :D

    [autoit]

    #include<Array.au3>
    Dim $array[3]
    $array[0] = "Test"
    $array[1] = "Bla"
    $array[2] = "test"

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

    MsgBox(0, "Found times 1", _countStringInArray1($array, 'Test'))
    MsgBox(0, "Found times 2", _countStringInArray1($array, 'Test', 2))
    MsgBox(0, "Found times 3", _countStringInArray1($array, 'Test', 0, 1))
    MsgBox(0, "Found times 4", _countStringInArray1 ($array, 'Test', 0, Default, 0))

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

    Func _countStringInArray1(Const ByRef $array, $search, $start = 0, $end = Default, $case = 1)
    If $case <> 1 Then $search = '(?i)' & $search
    $re = StringRegExp(_ArrayToString($array, ';', $start, $end), $search, 3)
    If Not @error Then Return UBound($re)
    EndFunc ;==>_countStringInArray1

    [/autoit]

    So long,

    Mega