Sign in to follow this  
Followers 0
notsure

$hEdit get end of line

15 posts in this topic

Hi, im kinda new to this so im sorry if i ask stupid questions. Im lookin in the helpfile as much as i can cause this helpfile rocks. But there's 1 thing i can't really find in the helpfile.

Im trying to parse a variable string in an EDITbox.

The string contains a variety of info which is dynamic. Example:

USER_NAME: blabla

USR_ADDRESS: whehehehe 25

USR_POSTALCODE: 1337XX

I assume you can understand an address is not always the same length. But the "fieldname" (in this case) "USR_ADDRESS:" is always the same. So i guess you have to find INSTR "USR_ADDRESS:". That seems logical to me but then how do i determine how long the string is behind that? So in this case "whehehehe 25" ?

Maybe it helps when i say that the info i need is always till the end of the line. So nothing comes after the "25" in the address string.

Thanks in advance for any answer :)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you can always use StringTrimLeft($string_to_trim, (number od characters to trim))

example:

$read_data = "USER_NAME: blabla" ;string you read from a file or edit control
$username = StringTrimLeft($read_data, 10) ;trim 10 left characters (including ":")
if StringLeft($username, 1) = " " then $username = StringTrimLeft($username, 1) ; cut off an empty space if there is one
ConsoleWrite($username & @CRLF) ;display data in the console, @CRLF is not needed in your case, it just makes a new line in the console

...or you can use StringRegExp, which could give you your username in one line instead of these 4 above, but as a newbie you should stick with the simple string commands.

Edited by sandin

Share this post


Link to post
Share on other sites

you can always use StringTrimLeft($string_to_trim, (number od characters to trim))

example:

$read_data = "USER_NAME: blabla" ;string you read from a file or edit control
$username = StringTrimLeft($read_data, 10) ;trim 10 left characters (including ":")
if StringLeft($username, 1) = " " then $username = StringTrimLeft($username, 1) ; cut off an empty space if there is one
ConsoleWrite($username & @CRLF) ;display data in the console, @CRLF is not needed in your case, it just makes a new line in the console

...or you can use StringRegExp, which could give you your username in one line instead of these 4 above, but as a newbie you should stick with the simple string commands.

Ok, i understand your input. Thanks for that. But it isnt quite exactly what i mean. Let me try to make it more clear.

Posted Image

How to filter "blabla" in this situation, if you are not sure about the length of the text behind "USER_NAME" and you are not sure IF "USR_ADDRESS" comes first. So you need to filter to the "end of the line" on the "USER_NAME"-line.

Share this post


Link to post
Share on other sites

notsure,

How does the text get into the editbox? Is it typed in by a user - in which case there is a good chance that USER_NAME will be spelt wrongly! - or does it come from a file somewhere?

The answer to your original question will differ greatly depending on how you get the information in the first place!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

notsure,

How does the text get into the editbox? Is it typed in by a user - in which case there is a good chance that USER_NAME will be spelt wrongly! - or does it come from a file somewhere?

The answer to your original question will differ greatly depending on how you get the information in the first place!

M23

It will be copy-pasted from another application at first. So someone will copy the text into the textbox and then the text must be parsed, thats what im trying to do so we only leave "usable" information behind in the textbox. It will be a HUGE lap of text, this is only an example :) Edited by notsure

Share this post


Link to post
Share on other sites

try reordering data you pasted into edit

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiEdit.au3>

$Form1 = GUICreate("Copy your data here:", 400, 200)
$Edit1 = GUICtrlCreateEdit("USER_NAME: blabla" & @CRLF & "USR_ADDRESS: whehehehe 25" & @CRLF & "USR_POSTALCODE: 1337XX", 0, 0, 400, 175)
$Button1 = GUICtrlCreateButton("Get Username", 0, 175, 100)
$Button2 = GUICtrlCreateButton("Get Address", 100, 175, 100)
$Button3 = GUICtrlCreateButton("Get PostalCode", 200, 175, 100)
$Button4 = GUICtrlCreateButton("Exit", 300, 175, 100)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Button4
            Exit
        Case $Button1
            if GUICtrlRead($Edit1) <> "" Then
                for $i = 0 to _GUICtrlEdit_GetLineCount($Edit1)
                    Local $got = _GUICtrlEdit_GetLine($Edit1, $i)
                    if StringLeft($got, 10) = "USER_NAME:" Then
                        
                        Local $username = StringTrimLeft($got, 10)
                        if StringLeft($username, 1) = " " then $username = StringTrimLeft($username, 1)
                        ConsoleWrite($username & @CRLF)
                        ExitLoop
                    EndIf
                Next
            EndIf
        Case $Button2
            if GUICtrlRead($Edit1) <> "" Then
                for $i = 0 to _GUICtrlEdit_GetLineCount($Edit1)
                    Local $got = _GUICtrlEdit_GetLine($Edit1, $i)
                    if StringLeft($got, 12) = "USR_ADDRESS:" Then
                        
                        Local $address = StringTrimLeft($got, 12)
                        if StringLeft($address, 1) = " " then $address = StringTrimLeft($address, 1)
                        ConsoleWrite($address & @CRLF)
                        ExitLoop
                    EndIf
                Next
            EndIf
        Case $Button3
            if GUICtrlRead($Edit1) <> "" Then
                for $i = 0 to _GUICtrlEdit_GetLineCount($Edit1)
                    Local $got = _GUICtrlEdit_GetLine($Edit1, $i)
                    if StringLeft($got, 15) = "USR_POSTALCODE:" Then
                        
                        Local $postal = StringTrimLeft($got, 15)
                        if StringLeft($postal, 1) = " " then $postal = StringTrimLeft($postal, 1)
                        ConsoleWrite($postal & @CRLF)
                        ExitLoop
                    EndIf
                Next
            EndIf
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Brilliant, that's what i mean yes. Can you remark the code a little so i can understand what happens exactly? I can read it like half... despite im experienced with VB and C++ code i cant understand this exactly

Im kinda new to this

Thanks alot anyhow :)

Edited by notsure

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Aaah, nevermind :) i can see how it works now.

if GUICtrlRead($Edit1) <> "" Then
                for $i = 0 to _GUICtrlEdit_GetLineCount($Edit1); get the linecount of the whole textbox and for/next till it's at its end 
                    Local $got = _GUICtrlEdit_GetLine($Edit1, $i); $got is supposed to get the whole line, based on $i, next IF-construction will determine its address we are trying to find
                    if StringLeft($got, 12) = "USR_ADDRESS:" Then;if so.. then go in 
                        
                        Local $address = StringTrimLeft($got, 12);trim 12 chars so USR_ADDRESS: will vanish
                        if StringLeft($address, 1) = " " then $address = StringTrimLeft($address, 1); if space before the real address, trim it off, delete.
                        ConsoleWrite($address & @CRLF);console pwns the address which is left.
                        ExitLoop
                    EndIf
                Next
            EndIf

Correct me if im wrong :)

Edited by notsure

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

yes, you got it.

you can also move it all into 1 button:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiEdit.au3>

$Form1 = GUICreate("Copy your data here:", 400, 200)
$Edit1 = GUICtrlCreateEdit("USER_NAME: blabla" & @CRLF & "USR_ADDRESS: whehehehe 25" & @CRLF & "USR_POSTALCODE: 1337XX", 0, 0, 400, 175)
$Button1 = GUICtrlCreateButton("Get all data", 0, 175, 100)
$Button4 = GUICtrlCreateButton("Exit", 300, 175, 100)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Button4
            Exit
        Case $Button1
            if GUICtrlRead($Edit1) <> "" Then
                for $i = 0 to _GUICtrlEdit_GetLineCount($Edit1)
                    Local $got = _GUICtrlEdit_GetLine($Edit1, $i)
                    if StringLeft($got, 10) = "USER_NAME:" Then
                        Local $username = StringTrimLeft($got, 10)
                        if StringLeft($username, 1) = " " then $username = StringTrimLeft($username, 1)
                        ConsoleWrite("username is: " & $username & @CRLF)
                        
                    ElseIf StringLeft($got, 12) = "USR_ADDRESS:" Then
                        Local $address = StringTrimLeft($got, 12)
                        if StringLeft($address, 1) = " " then $address = StringTrimLeft($address, 1)
                        ConsoleWrite("address is: " & $address & @CRLF)
                        
                    ElseIf StringLeft($got, 15) = "USR_POSTALCODE:" Then
                        Local $postal = StringTrimLeft($got, 15)
                        if StringLeft($postal, 1) = " " then $postal = StringTrimLeft($postal, 1)
                        ConsoleWrite("postal is: " & $postal & @CRLF)
                    EndIf
                Next
                ConsoleWrite(">------------" & @CRLF)
            EndIf
    EndSwitch
WEnd
Edited by sandin

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Regular expression is a better solution.

$var = "USER_NAME: blabla" & @CRLF & "USR_ADDRESS: whehehehe 25" & @CRLF & "USR_POSTALCODE: 1337XX"
 
 $a1 = StringRegExp($var,'(?i:USER_NAME: )(\V*)',1)
 $USER_NAME = $a1[0]
 
 $a2 = StringRegExp($var,'(?i:USR_ADDRESS: )(\V*)',1)
 $USR_ADDRESS = $a2[0]
 
 $a3 = StringRegExp($var,'(?i:USR_POSTALCODE: )(\V*)',1)
 $USR_POSTALCODE = $a3[0]
 
 ConsoleWrite($USER_NAME & @CRLF & $USR_ADDRESS & @CRLF & $USR_POSTALCODE & @CRLF)
Edited by weaponx

Share this post


Link to post
Share on other sites

Yet another way to skin your cat:

; INITIALIZATION----------------------------------------------------------------
Const $Field_Types = 3
Const $Field_Type[3][2] = [["USER_NAME: ",11],["USR_ADDRESS: ",13],["USR_POSTALCODE: ",16]] 
Global $Current_Record, $Record_Count, $EOF

; MAIN -------------------------------------------------------------------------
;Open_File()
While 1
    Read_Record()
    If $EOF Then ExitLoop
    For $x = 0 to $Field_Types - 1
        If StringLeft($Current_Record, $Field_Type[$x][1]) = $Field_Type[$x][0] Then
            Call(StringLeft($Field_Type[$x][0], $Field_Type[$x][1] - 2),StringTrimLeft($Current_Record, $Field_Type[$x][1]))
            ExitLoop
        EndIf
    Next
WEnd
;Close_File()
;Print_Report()
Exit

; FUNCTIONS --------------------------------------------------------------------
Func USER_NAME($data)
    MsgBox(1,"","Processing USER_NAME: " & $data)
EndFunc

Func USR_ADDRESS($data)
    MsgBox(1,"","Processing USR_ADDRESS: " & $data)
EndFunc

Func USR_POSTALCODE($data)
    MsgBox(1,"","Processing USR_POSTALCODE: " & $data)
EndFunc

Func Read_Record(); Simulated reading lines of data from file or control
    Local $Edit1[6] = ["USER_NAME: plugh","USR_ADDRESS: 25 Plover St.","USR_POSTALCODE: 133777","USER_NAME: xyzzy","USR_ADDRESS: 1 Foobar Ln.","USR_POSTALCODE: 222222"]
    If $Record_Count = UBound($Edit1) Then
        $EOF = 1
    Else
        $Current_Record = $Edit1[$Record_Count]
        $Record_Count += 1
    EndIf
EndFunc

If you wanted to add, for instance, a "USR_PHONE" field, you'd just add an entry to the table at top, and write a "Func USR_PHONE()"

Share this post


Link to post
Share on other sites

Spiff59, i'm thinking you are way off base...I'm not trying to sound mean but you have taken the simplest steps and overcomplicated them.

Share this post


Link to post
Share on other sites

Spiff59, i'm thinking you are way off base...I'm not trying to sound mean but you have taken the simplest steps and overcomplicated them.

I tend to disagree. I suppose the statement:

If StringLeft($Current_Record, $Field_Type[$x][1]) = $Field_Type[$x][0] Then
            Call(StringLeft($Field_Type[$x][0], $Field_Type[$x][1] - 2),StringTrimLeft($Current_Record, $Field_Type[$x][1]))

is somewhat complex, but overall I've presented a version with little hard-coding that is very structured and easily modified.

Share this post


Link to post
Share on other sites

Okay let me explain.

First you ignore the existence of _FileReadToArray().

Secondly, your implementation of line by line file reading is bass ackwards. You declare an array inside of Read_Record() which is supposed to be simulating a file, yet when you read a file in the line breaks are still in place. This array is already split by line breaks which would require another function (See: _FileReadToArray).

Third, You are assuming the contents will be in order. Using a regular expression allows the values to be on any line.

Finally you are using Call() to 3 of the same exact function when you only need 1 function with 2 parameters.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Okay let me explain.

First you ignore the existence of _FileReadToArray().

Secondly, your implementation of line by line file reading is bass ackwards. You declare an array inside of Read_Record() which is supposed to be simulating a file, yet when you read a file in the line breaks are still in place. This array is already split by line breaks which would require another function (See: _FileReadToArray).

Third, You are assuming the contents will be in order. Using a regular expression allows the values to be on any line.

Finally you are using Call() to 3 of the same exact function when you only need 1 function with 2 parameters.

I could have just stuck his data examples in a string and run them through a loop to parse them out to separate functions, but I thought I'd provide a more complete example. I'm familiar with _FileReadToArray. I don't know what more, beyond the comment, that I could have done to indicate the Read_Record() function was just test code, since it's not clear to me where the OP will actually be getting his data, whether from a control, the clipboard, or a file. Assaulting a trivial part of the example seems like nitpicking.

The example makes no assumption as to the order of the data.

I *am* assuming the OP might want to do more than a ConsoleWrite with his data elements, so routing them to separate functions for processing seems a well-structured approach.

Anyway, I respect your opinion, and my first post started with "Another way to skin your cat:", which I derived from the phrase "There's a 1000 ways to skin a cat".

Edit: Split reply from 2 to 4 paras, for a more point-by-point look :)

Edited by Spiff59

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
Sign in to follow this  
Followers 0