Jump to content
CaptainSparky

StringLeft help!

Recommended Posts

CaptainSparky

Hello good friends of Autoit, so I am making a chatbot using Autoit but I got stuck on this part:

I already have stringright($var,stringlen($var) - stringlen(" = ") - StringInStr($var," = ") + 1) the program is made to read a text file and extract from a specific line the text after = and before =, for example:

example line of text file: something = okay this is a text

and I needed to extract the right part (which I already have done) and the left part (which I need help with) so how can you make stringleft to get all text before "="?

Thanks in advance.

Here's my code if you need it:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Misc.au3>
#Include <Array.au3>

Global $BrainLocation = @ScriptDir & "\brain.txt"
Global $FormName = "Project Lisa"
Global $ReatInput = "null"
Global $Memory[100]
Global $BrainLineCount = 1
Global $Random[100]
Global $RandomCount = 0
Global $SplitInput[100]
Global $BrainCount = 0

Loader()

Func Loader()

    Global $Form = GUICreate($FormName, 417, 279, 192, 124)
    Global $Output = GUICtrlCreateEdit("", 8, 8, 401, 225, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_HSCROLL),0)
    Global $Input = GUICtrlCreateInput("", 104, 240, 209, 21)
    GUISetState(@SW_SHOW)
    ControlFocus($FormName,"",$input)
    HotKeySet("{ENTER}","SendText")

    Main()

EndFunc

func Main()

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd

EndFunc


Func SendText()

    $ReatInput = GUICtrlRead($Input)
    GUICtrlSetData($Output,@CRLF & "You > " & $ReatInput,1)

    CreateRandoms()

EndFunc

Func CreateRandoms()

    While $RandomCount <> 100
        $Random[$RandomCount] = Random(1,$RandomCount,1)
        $RandomCount += 1
    WEnd
    $RandomCount = 0

    BrainLoader()

EndFunc

Func BrainLoader()

    $ReatBrain = FileReadLine($Brainlocation,$BrainLineCount)
    $BrainStringRight = stringright($ReatBrain,stringlen($ReatBrain) - stringlen(" = ") - StringInStr($ReatBrain," = ") + 1)
    MsgBox(0,"",$Brainstringright)
    $BrainStringLenRight = Stringlen($brainstringright)
    MsgBox(0,"",$Brainstringright)
    $BrainActualString = 0 + $BrainStringLenRight - 10
    MsgBox(0,"",$BrainActualString)
    $test = StringLeft($ReatBrain,$brainactualstring)
    MsgBox(0,"",$test)
    $BrainSplitedStrings = StringSplit($BrainStringRight,",")

    If $BrainSplitedStrings[1] <> "" Then
        $_1 = "true"
        $BrainCount += 1
    EndIf

    If $ReatInput = $BrainActualString Then
        $BrainRan = Random(1,$braincount)
        GUICtrlSetData($output,@CRLF & "Lisa > " & $BrainSplitedStrings[$BrainRan],1)
        $BrainLineCount = 1
        Main()
    Else
        main()
    EndIf
    $BrainLineCount += 1
    BrainLoader()

EndFunc

Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

(Think) BEFORE you post.

Share this post


Link to post
Share on other sites
javiwhite

if you need both parts of the text, why don't you just use stringsplit?

$text = "some = text"
$Split = StringSplit($text,"=")
ConsoleWrite($Split[1]) ;Some
ConsoleWrite(@CR)
ConsoleWrite($Split[2]) ;Text

Many Thanks

Javi

  • Like 1

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites
CaptainSparky

Holy cow! HOW DID I DIDN'T THINK OF THAT? how stupid of me. Thank you good friend.


Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

(Think) BEFORE you post.

Share this post


Link to post
Share on other sites
Skysnake

Also look at the Regular Expressions?


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites
TheDcoder

You might wanna look at my StringTrimRightUntil & StringTrimLeftUntil functions here

They are more efficient & simple! TD :D


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
CaptainSparky

Thank you guys.


Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

(Think) BEFORE you post.

Share this post


Link to post
Share on other sites
CaptainSparky

Sorry that didn't work out... I need the opposite ( the left version ) of this one : stringright($var,stringlen($var) - stringlen(" = ") - StringInStr($var," = ") + 1)

I need to stringleft after/before a specific character(s) for example in the one above is after =, the one I need should be Get all text BEFORE =.

anyone? thanks in advance :)

Edited by TheNewHunter

Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

(Think) BEFORE you post.

Share this post


Link to post
Share on other sites
CaptainSparky

Okay I got it guys.


Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

(Think) BEFORE you post.

Share this post


Link to post
Share on other sites
javiwhite

StringSplit all the way my friend, Even if it's just for ease of reading when you come back to a code a few months later (it's amazing how a solution to a problem you face can seem innovative at the time, But then Jargon when you come back to the code.) Even if you're just looking for the left part of the string, StringSplit can accomplish this:

$text = "some = text"
$Left = StringSplit($text,"=")[1] ;Notice the array number
$Right = StringSplit($text,"=")[2] ;These run the stringsplit command, And then return that element to the variable from the resulting array
Consolewrite($Left)
ConsoleWrite(@CR)
ConsoleWrite($Right)

Just a handy little trick I discovered after a while of using AutoIt, The same applies to any commands that return arrays.

Cheers

Javi

  • Like 1

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites
MikahS

@TheNewHunter Make sure you post your solution, so that anyone that has the same problem can see how you solved it. ;)

  • Like 1

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
Malkey

Also look at the Regular Expressions?

Using a regular expression allows control over optional spaces before and after the equal sign, and having those spaces (if they exist) not appearing in the required 'before and after' text.

$text = "some = text"

; These StringRegExpReplace replaces all text matched with nothing, "".   (Match the characters to be deleted)

$Left = StringRegExpReplace($text, "\h*=.*$", "")
; "\h*"  Match all spaces (if they exist) immediately before "=".   Note:- A space is a character, and can be classified as a horizontal whitespace character.
; "="    Match the equal character.
; ".*$"  Match all characters (if any exist) to the end of the test string (from immediately after "=").

$Right = StringRegExpReplace($text, "^.*=\h*", "")
; "^.*"  Match all characters (if any exist) from the beginning of the test string to immediately before "=".
; "="    Match the equal character.
; "\h*"  Match all spaces (if they exist) immediately after "=".
; See AutoIt help file under StringRegExp for regular expression syntax.

ConsoleWrite("Left:  " & $Left & @LF)
ConsoleWrite("Right: " & $Right & @LF)


@javiwhite
Another handy little trick, if you are writing your scripts in SciTE, is to type in lower case "cw ".  That is, type"cw" followed by a space. This is the SciTE4AutoIt3 abbreviation for "ConsoleWrite(| & @LF) ".  I use it often.  
The complete list of SciTe-AutoIt abbreviations can be found in the SciTe help file > under the "Index" tab  > SciTE4AutoIt3-AbbreviationList.
 

  • Like 2

Share this post


Link to post
Share on other sites
iamtheky

Just a handy little trick I discovered after a while of using AutoIt, The same applies to any commands that return arrays.

You have no validation that stringsplit succeeded and risk throwing an exception attempting to access an element that does not exist, so you may want to do a stringinstr to verify an equals sign is present prior to running that.  or just split it once and check for an error then access the elements.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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

  • Similar Content

    • Schneeflocke
      By Schneeflocke
      #RequireAdmin
      #include <NomadMemory.au3>
      $Value = _Pointer()
      MsgBox(0,"", "Value: " = $Value)
      Func _Pointer()
      Global $iBase = 0x7FF649DB0000, $iStaticofsset = 0x01559C08
      Global $hOpen, $sRead
      $hOpen = _MemoryOpen(ProcessExists("Tutorial-i386.exe"))
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x70
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0xA0
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x20
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x1B0
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x40
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x10
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x368
      $sValue = _Memoryread($sRead, $hOpen, 'Float')
      _MemoryClose($hOpen)
      Return $sValue
      EndFunc
      I Would like to create an Round Timer and Kers BoT for Assetto Corsa.
      Intelliboost need too many Performance
    • Pagi
      By Pagi
       
      <a href="index.php=4143b27b41db0a40c499202865c3e24d" class="menuitem " target="\_self"> "Menu Item 1" </a>
      Can you tell me if it´s possible to click this menu button? If yes, how? Cause I have no idea, I think i should use _IETagNameGetCollection
    • milkmoron
      By milkmoron
      https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/?do=findComment&comment=1156373 At least we have an element title: [Entered By:] class: [WindowsForms10.EDIT.app.0.30495d1_r9_ad1] Having the following values for all properties: Title is: <Entered By:> Class := <WindowsForms10.EDIT.app.0.30495d1_r9_ad1> controltype:= <UIA_EditControlTypeId> ,<50004> , (0000C354) 322;370;736;184 *** Parent Information top down *** 3: Title is: <View Activity> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_WindowControlTypeId> ,<50032> , (0000C370) 306;72;784;605 "Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 2: Title is: <> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;103;768;542 "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 1: Title is: <> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;103;749;451 "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 0: Title is: <Delete> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;122;749;432 "Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" ;~ *** Standard code maintainable *** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) _UIA_setVar("oP1","Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ;View Activity _UIA_setVar("oP2","Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ; _UIA_setVar("oP3","Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ; _UIA_setVar("oP4","Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ;Delete _UIA_setVar("EnteredBy:.mainwindow","title:=Entered By:;classname:=WindowsForms10.EDIT.app.0.30495d1_r9_ad1") ;~ Actions split away from logical/technical definition above can come from configfiles ;~_UIA_Action("oP1","highlight") _UIA_Action("oP1","setfocus") ;~_UIA_Action("oP2","highlight") _UIA_Action("oP2","setfocus") ;~_UIA_Action("oP3","highlight") _UIA_Action("oP3","setfocus") ;~_UIA_Action("oP4","highlight") _UIA_Action("oP4","setfocus") _UIA_action("EnteredBy:.mainwindow","setfocus") ;~ *** Standard code Flexible*** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) Local $oP3=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP3,"setfocus") Local $oP2=_UIA_getObjectByFindAll($oP3, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP2,"setfocus") Local $oP1=_UIA_getObjectByFindAll($oP2, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP1,"setfocus") Local $oP0=_UIA_getObjectByFindAll($oP1, "Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP0,"setfocus") _UIA_setVar("EnteredBy:.mainwindow","title:=Entered By:;classname:=WindowsForms10.EDIT.app.0.30495d1_r9_ad1") _UIA_action("EnteredBy:.mainwindow","setfocus") Need some help selecting this defining it as a variable to save and then insert the text within, to the same application but different account.
    • PleaseHelpMEIWillLoveyou
      By PleaseHelpMEIWillLoveyou
      Is there a way to use a custom terminal or something from my pc and run a script on another pc.
      please let me know if its possible
      (not for anything bad just for a project im working on)
    • kelso
      By kelso
      Hello Guru's,
       
      I'm trying to write an autoit script to select from the dropdown list as you see in the attached picture. 
      I read the help page for _IEFormElementOptionSelect, but I cannot grasp how to correlate that with the source code that I'm seeing. any suggestions?

×