notsure Posted February 6, 2009 Share Posted February 6, 2009 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 Link to comment Share on other sites More sharing options...
sandin Posted February 6, 2009 Share Posted February 6, 2009 (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 February 6, 2009 by sandin Some cool glass and image menu | WinLIRC remote controler | Happy Holidays to all... | Bounce the sun, a game in which you must save the sun from falling by bouncing it back into the sky | Hook Leadtek WinFast TV Card Remote Control Msges | GDI+ sliding toolbar | MIDI Keyboard (early alpha stage, with lots of bugs to fix) | Alt+Tab replacement | CPU Benchmark with pretty GUI | Ini Editor - Edit/Create your ini files with great ease | Window Manager (take total control of your windows) Pretty GUI! | Pop-Up window from a button | Box slider for toolbar | Display sound volume on desktop | Switch hotkeys with mouse scroll Link to comment Share on other sites More sharing options...
notsure Posted February 6, 2009 Author Share Posted February 6, 2009 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. 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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 6, 2009 Moderators Share Posted February 6, 2009 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
notsure Posted February 6, 2009 Author Share Posted February 6, 2009 (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!M23It 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 February 6, 2009 by notsure Link to comment Share on other sites More sharing options...
sandin Posted February 6, 2009 Share Posted February 6, 2009 try reordering data you pasted into edit expandcollapse popup#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 Some cool glass and image menu | WinLIRC remote controler | Happy Holidays to all... | Bounce the sun, a game in which you must save the sun from falling by bouncing it back into the sky | Hook Leadtek WinFast TV Card Remote Control Msges | GDI+ sliding toolbar | MIDI Keyboard (early alpha stage, with lots of bugs to fix) | Alt+Tab replacement | CPU Benchmark with pretty GUI | Ini Editor - Edit/Create your ini files with great ease | Window Manager (take total control of your windows) Pretty GUI! | Pop-Up window from a button | Box slider for toolbar | Display sound volume on desktop | Switch hotkeys with mouse scroll Link to comment Share on other sites More sharing options...
notsure Posted February 9, 2009 Author Share Posted February 9, 2009 (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 February 9, 2009 by notsure Link to comment Share on other sites More sharing options...
notsure Posted February 9, 2009 Author Share Posted February 9, 2009 (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 February 9, 2009 by notsure Link to comment Share on other sites More sharing options...
sandin Posted February 9, 2009 Share Posted February 9, 2009 (edited) yes, you got it. you can also move it all into 1 button: expandcollapse popup#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 February 9, 2009 by sandin Some cool glass and image menu | WinLIRC remote controler | Happy Holidays to all... | Bounce the sun, a game in which you must save the sun from falling by bouncing it back into the sky | Hook Leadtek WinFast TV Card Remote Control Msges | GDI+ sliding toolbar | MIDI Keyboard (early alpha stage, with lots of bugs to fix) | Alt+Tab replacement | CPU Benchmark with pretty GUI | Ini Editor - Edit/Create your ini files with great ease | Window Manager (take total control of your windows) Pretty GUI! | Pop-Up window from a button | Box slider for toolbar | Display sound volume on desktop | Switch hotkeys with mouse scroll Link to comment Share on other sites More sharing options...
weaponx Posted February 9, 2009 Share Posted February 9, 2009 (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 February 9, 2009 by weaponx Link to comment Share on other sites More sharing options...
Spiff59 Posted February 9, 2009 Share Posted February 9, 2009 Yet another way to skin your cat: expandcollapse popup; 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()" Link to comment Share on other sites More sharing options...
weaponx Posted February 9, 2009 Share Posted February 9, 2009 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. Link to comment Share on other sites More sharing options...
Spiff59 Posted February 9, 2009 Share Posted February 9, 2009 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. Link to comment Share on other sites More sharing options...
weaponx Posted February 9, 2009 Share Posted February 9, 2009 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. Link to comment Share on other sites More sharing options...
Spiff59 Posted February 9, 2009 Share Posted February 9, 2009 (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 February 9, 2009 by Spiff59 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now