Jump to content

Recommended Posts

Posted

Hey guys! I am a newbie that wants to make my job easy. Can you guys help me out?
I want to get one word in a paragraph or in a line randomly. What command should i use?
For example, I want to extract the word "Global" in the sentence.
Sentence: Internet as a Global solution.

 

Looking forward to your answers,thanks!

Posted (edited)

This will take text, split it up into an array, then display a random word from that array.

$sTextToSearch = "If you use an empty string "" for the delimiters, each character will be returned as an element." & @CRLF & _
"If the delimiter string contains several characters function behaviour depends on the flag setting. If set to $STR_CHRSPLIT the string will be split at every instance of each of the individual characters in the delimiter - if set to $STR_ENTIRESPLIT then the string will only be split when the entire delimiter string is encountered. See second example below." & @CRLF & _
"Note that the macro @CRLF is actually a 2 character string, so unless the flag parameter to $STR_ENTIRESPLIT extra blank lines will be generated as the string is split on both @CR and @LF."

$sTextNoCR = StringReplace($sTextToSearch, @CRLF, " ")
$sText = StringStripWS($sTextNoCR, 4)
$aWords = StringSplit($sTextToSearch, " ")
MsgBox(0, "Random Word", $aWords[Random(0, UBound($aWords) - 1)])

 

Edited by BigDaddyO
switched StringStripCR with StringReplace
Posted (edited)
Quote

Hey guys! I am a newbie that wants to make my job easy. Can you guys help me out?
I want to get one word in a paragraph or in a line randomly. What command should i use?
For example, I want to extract the word "Global" in the sentence.
Sentence: Internet as a Global solution.

 

Hello fabianelucio,
I have a couple of examples. As you state, you are new to AutoIt, and therefore possibly new to programming. These examples may or will seem advanced to you. However; I spent the time, and others may benefit, so here they are.

Quote

For example, I want to extract the word "Global" in the sentence.
Sentence: Internet as a Global solution.

 

Example 1: Extract the word "Global"

#include <StringConstants.au3>

Global $stringToSearch = "Internet as a Global solution."
Global $stringToFind = "Global"
Global $aRet = StringRegExp($stringToSearch, $stringToFind, $STR_REGEXPARRAYMATCH)

If @error = 1 Then
    MsgBox(0, "Match not Found", "Not found")
Else
    MsgBox(0, "Match Found", $aRet[0])
EndIf

Exit

 

 

Example 2: Find a word in a paragraph.

This one provides a GUI with a single line input control. an edit control, and a few buttons; Find, Reset, Exit.

The edit control is auto populated with text. Using the single line input, one can enter a word to "Find" in the text body / paragraph. If a word is found, the word is highlighted. The Reset button clears the single line input, and deselects highlighted text. The Exit button closes the program.

#include <GUIConstants.au3>
#include <FontConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

AutoItSetOption("GUIOnEventMode", 0)
AutoItSetOption("MustDeclareVars", 1)
;
;
;
;
_Main()
;
;
;
;
; #### Function ############################################################
; # Name .............: _Main()
; # Description ......: Entry point for this graphical Windows-based
; #                     program.
; #                     Initialize the program, display its main window, and
; #                     its controls, and enter a message retrieval-and-dispatch
; #                     loop, which is the top-level control structure for the
; #                     program’s execution.
; # ..................:
; # Syntax ...........: _Main()
; # Parameter(s) .....:
; # Return value(s) ..:
; # Requirement(s) ...: AutoIt 3.3.16.1
; # Author(s) ........:
; # Created ..........: October 12, 2024
; # Modified .........: October 12, 2024
; # Remarks ..........:
; # Related ..........:
; # Link(s) ..........:
; # Example ..........:
; # Notes ............:
; ##########################################################################
Func _Main()
    ;String to search.
    ;String harvested from the AutoIt landing page. https://www.autoitscript.com/site/
    Local $string = "AutoIt v3 is a freeware BASIC-like scripting language "
    $string &= "designed for automating the Windows GUI and general scripting. "
    $string &= "It uses a combination of simulated keystrokes, mouse movement "
    $string &= "and window/control manipulation in order to automate tasks in "
    $string &= "a way not possible or reliable with other languages."

    Local $hWndMain = GUICreate("Example: Find String", 500, 280, -1, -1, -1, -1)

    Local $cid_Input = GUICtrlCreateInput("", 10, 10, 420, 30, -1, -1)
    GUICtrlSetFont($cid_Input, 12, $FW_NORMAL , $GUI_FONTNORMAL, "Verdana", $CLEARTYPE_QUALITY)

    Local $cid_BtnFind = GUICtrlCreateButton("Find", 440, 10, 50, 30, -1, -1)
    GUICtrlSetFont($cid_BtnFind, 12, $FW_NORMAL , $GUI_FONTNORMAL, "Verdana", $CLEARTYPE_QUALITY)

    Local $cid_BtnReset = GUICtrlCreateButton("Reset", 10, 190, 54, 30, -1, -1)
    GUICtrlSetFont($cid_BtnReset, 12, $FW_NORMAL , $GUI_FONTNORMAL, "Verdana", $CLEARTYPE_QUALITY)

    Local $cid_BtnExit = GUICtrlCreateButton("Exit", 10, 240, 54, 30, -1, -1)
    GUICtrlSetFont($cid_BtnExit, 12, $FW_NORMAL , $GUI_FONTNORMAL, "Verdana", $CLEARTYPE_QUALITY)

    Local $cid_Edit = GUICtrlCreateEdit("", 10, 60, 480, 120, BitOR($ES_NOHIDESEL, $ES_WANTRETURN, $ES_MULTILINE, $WS_VSCROLL), -1)
    GUICtrlSetFont($cid_Edit, 12, $FW_NORMAL, $GUI_FONTNORMAL, "Verdana", $CLEARTYPE_QUALITY)
    GUICtrlSetData($cid_Edit, $string)

    GUISetState(@SW_SHOW, $hWndMain)

    Local $iGuiMsg

    While 1
        $iGuiMsg = GUIGetMsg()
        Switch $iGuiMsg
            Case $GUI_EVENT_CLOSE, $cid_BtnExit
                ExitLoop
            Case $cid_BtnFind
                _Find($hWndMain, $cid_Input, $cid_Edit)
            Case $cid_BtnReset
                _Reset($cid_Input, $cid_Edit)
        EndSwitch
    WEnd
    GUIDelete($hWndMain)
EndFunc ;==> _Main()
;
;
;
;
; #### Function ############################################################
; # Name .............: _Find()
; # Description ......: Find a string in a string, and highlight the found
; #                     string within the text in the Edit control.
; #                     If the string that was searched for, was not found,
; #                     alert the program user. Then clear the Input Control
; #                     and set focus to the Input Control.
; # ..................:
; # Syntax ...........: _Find($hWnd, $id_Input, $id_Edit)
; # Parameter(s) .....: @param: $hWnd - Handle to the parent window.
; #                     @param: $id_Input - Control id of the Input Control.
; #                     @param: $id_Edit - Control id of the Edit Control.
; # Return value(s) ..:
; # Requirement(s) ...: AutoIt 3.3.16.1
; # Author(s) ........:
; # Created ..........: October 12, 2024
; # Modified .........: October 12, 2024
; # Remarks ..........:
; # Related ..........:
; # Link(s) ..........: https://learn.microsoft.com/en-us/windows/win32/controls/em-setsel
; # Example ..........:
; # Notes ............:
; ##########################################################################
Func _Find($hWnd, $id_Input, $id_Edit)
    Local $sFindString = GUICtrlRead($id_Input)

    Local $iFindStrLength = StringLen($sFindString)

    Local $sSearch = GUICtrlRead($id_Edit)

    Local $iStringCharPosition = StringInStr($sSearch, $sFindString, $STR_NOCASESENSE)

    Local $iSelStartPos = ($iStringCharPosition - 1)

    Local $iSelEndPos = ($iSelStartPos + $iFindStrLength)

    If $iStringCharPosition = 0 Then
        MsgBox($MB_ICONINFORMATION + $MB_OK, "Message", "String not found.", "", $hWnd)
        GUICtrlSetData($id_Input, "")
        GUICtrlSetState($id_Input, $GUI_FOCUS)
        Return
    Else
        ; Highlight the found string.
        GUICtrlSendMsg($id_Edit, $EM_SETSEL, $iSelStartPos, $iSelEndPos)
    EndIf

    Return
EndFunc ;==> _Find()
;
;
;
;
; #### Function ############################################################
; # Name .............: _Reset()
; # Description ......: Reset clears the single line Input control, deselects
; #                     highlighted string found within the text in the
; #                     Edit control, if there is one, and sets the Input control to
; #                     be the control which is focused.
; # ..................:
; # Syntax ...........: _Reset($id_Input, $id_Edit)
; # Parameter(s) .....: @param: $id_Input - Control id of the Input Control.
; #                     @param: $id_Edit - Control id of the Edit Control.
; # Return value(s) ..:
; # Requirement(s) ...: AutoIt 3.3.16.1
; # Author(s) ........:
; # Created ..........: October 12, 2024
; # Modified .........: October 12, 2024
; # Remarks ..........:
; # Related ..........:
; # Link(s) ..........: https://learn.microsoft.com/en-us/windows/win32/controls/em-setsel
; # Example ..........:
; # Notes ............:
; ##########################################################################
Func _Reset($id_Input, $id_Edit)
    GUICtrlSetData($id_Input, "")
    GUICtrlSendMsg($id_Edit, $EM_SETSEL, -1, -1)
    GUICtrlSetState($id_Input, $GUI_FOCUS)
    Return
EndFunc ;==> _Reset()

 

Edited by AndrewG

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
×
×
  • Create New...