Jump to content

how to randomly choose between only those lines of an ini that contain text?


Recommended Posts

Hello folks, I am again in need of your help.

I have written a script that reads variables from an ini file and at the press of a button randomly picks one such variable and outputs its content to notepad. In case a variable is empty, the script tries to output it too no matter what. This could result in no output at all, depending on how luckily the random function hits those empty variables.

One approach would be to make it so that as soon as an empty variable is found, the script tries again until a variable containing text to display is found. With further expansion of the script in mind, this method also has a major drawback: if the random function can choose between several hundred variables, and for example only one of them contains text to display, it might never show up at all in notepad or the computer appears to hang until this one specific variable has been picked.

So my goal is to have the script read only those lines that contain text, and only those be made available to choose from by the random function. To make things worse, the example ini posted below really is just an example, it might look completely different on another user's machine concerning empty lines and those containing information to display.

This is what I got:

ini file looks something like this:

[Section]
Key001=line 1
Key002=line 2
Key003=line 3
Key004=
Key005=line 5
Key006=line 6
Key007=
Key008=
Key009=line 9
Key010=

script is:

Global Const $VK_SCROLL = 0x91
$ini = @MyDocumentsDir & "\my ini.ini"
$section = "section"

Dim $messages[10]
$messages[0] = iniread($ini, $section, "Key001", "")
$messages[1] = iniread($ini, $section, "Key002", "")
$messages[2] = iniread($ini, $section, "Key003", "")
$messages[3] = iniread($ini, $section, "Key004", "")
$messages[4] = iniread($ini, $section, "Key005", "")
$messages[5] = iniread($ini, $section, "Key006", "")
$messages[6] = iniread($ini, $section, "Key007", "")
$messages[7] = iniread($ini, $section, "Key008", "")
$messages[8] = iniread($ini, $section, "Key009", "")
$messages[9] = iniread($ini, $section, "Key010", "")

While 1
   Sleep(10)
    If WinActive("Untitled") AND _IsPressed('01') = 1 AND _GetScrollLock() = 1 Then
      Do
      sleep(10) 
      Until _IsPressed('01') = 0
      Send($messages[Random(0, 9, 1)])
    EndIf
Wend

Func _IsPressed($hexKey)
    Local $aR, $bO
    $hexKey = '0x' & $hexKey
    $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
    If Not @error And BitAND($aR[0], 0x8000) = 0x8000 Then
        $bO = 1
    Else
        $bO = 0
    EndIf
    Return $bO
EndFunc

Func _GetScrollLock()
    Local $ret
    $ret = DllCall("user32.dll","long","GetKeyState","long",$VK_SCROLL)
    Return $ret[0]
EndFunc

Can you please lend me another hand on this one? Thank you.

Link to comment
Share on other sites

  • Moderators

Changed a few things:

Global Const $VK_SCROLL = 0x91
$ini = @MyDocumentsDir & "\my ini.ini"
$section = "section"

Dim $messages = IniReadSection($ini, $section), $RandomNum

While 1
    Sleep(10)
    If WinActive("Untitled") AND _IsPressed('01') AND _GetScrollLock() = 1 Then
        Do
        Sleep(10)
        Until Not _IsPressed('01')
        Do
            $RandomNum = Random(1, UBound($messages) - 1, 1)
        Until $RandomNum <> ''
        Send($messages[$RandomNum][1])
    EndIf
Wend

Func _IsPressed($v_R, $v_dll = 'user32.dll')
    $v_R = DllCall($v_dll, 'int', 'GetAsyncKeyState', 'int', '0x' & $v_R)
    Return (Not @error And BitAND($v_R[0], 0x8000) = 0x8000) * 1
EndFunc   ;==>_IsPressed

Func _GetScrollLock()
    Local $ret
    $ret = DllCall("user32.dll","long","GetKeyState","long",$VK_SCROLL)
    Return $ret[0]
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...