MrVining Posted February 23, 2013 Posted February 23, 2013 I have a file that changes somewhat frequently (at least 5 times when this section of my script runs) and I want my script to capture some data from it. The file is much longer, but I have copied the syntax completely with two entries. Every time the file is written it writes the entries in random order, so next time the script reads the file entry yyy could be above xxx. The only two variables I need to work with are "value2" and "value3". The file changes so much that I'm not even sure if it makes sense to convert it into an array, but this is a small part of my FIRST script so I'm a total newb. (I'm pretty excited about what I have learned so far, I have started to work with arrays, and my script is connecting to a remote mysql database and doing some other fun stuff). The script will have "xxx" and needs to find the number corresponding to it. What's worse (for me) is that it doesn't need the last 4 digits of the number. So the value currently associated with "xxx" is actually "24687". I'm not afraid to read, but I'd like some advice from some experienced people to point me at the right direction. aaa = { ["xxx_bbb::zzz"] = { ["value1"] = true, ["value2"] = "xxx", ["value3"] = 246870316, }, ["yyy_ccc::zzz"] = { ["value1"] = true, ["value2"] = "yyy", ["value3"] = 32608358, }, }
Moderators Melba23 Posted February 23, 2013 Moderators Posted February 23, 2013 (edited) MrVining, So you want to look for the digits "24687" in the file and then extract the "value2" and "value3" items in teh lines immediately above it? Is that correct? M23 Edit: If that is the case then this should work: #include <File.au3> #include <Array.au3> Global $aLines _FileReadToArray("data.txt", $aLines) $iIndex = _ArraySearch($aLines, "= 24687", 0, 0, 0, 1) If $iIndex <> -1 Then $sVal_3 = StringRegExpReplace($aLines[$iIndex], '.*\["(.*)"\].*', "$1") $sVal_2 = StringRegExpReplace($aLines[$iIndex - 1], '.*\["(.*)"\].*', "$1") EndIf MsgBox(0, "Result", $sVal_2 & @CRLF & $sVal_3) on this data: aaa = { ["xxx_bbb::zzz"] = { ["value11"] = true, ["value12"] = "xxx", ["value13"] = 246870316, }, ["yyy_ccc::zzz"] = { ["value21"] = true, ["value22"] = "yyy", ["value23"] = 32608358, }, } Edited February 23, 2013 by Melba23 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
water Posted February 23, 2013 Posted February 23, 2013 First thing you need to make sure is that the data you read is consistent. As the file changes to frequently how will you make sure that - after you have read block 1 - the file hasn't changed and block 2 is still valid data? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
MrVining Posted February 23, 2013 Author Posted February 23, 2013 @ Melba23, I need something like: Func NewFunc($var) Array_Magic($var) EndFunc $output = NewFunc("xxx") ; $output now = 24687 @ water, The script activates the change in the file up above in a different function, so I shoulc be good there. @ Melba23, I've started looking into that array include, I'm thinking that will be the ticket. The problem I'm running into with the code you listed is this symbol seems to be throwing me errors: {
MrVining Posted February 23, 2013 Author Posted February 23, 2013 Okay, I figured out my issue with your code, I didn't realize "#include <File.au3>" was not referring to the text file I was working with... /fail
Moderators Melba23 Posted February 23, 2013 Moderators Posted February 23, 2013 MrVining, So does the code work as you want? 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
MrVining Posted February 23, 2013 Author Posted February 23, 2013 Nope, I needed the opposite of what you gave me, but I'm getting it figured out! This scripting stuff is fun.
Moderators Melba23 Posted February 23, 2013 Moderators Posted February 23, 2013 MrVining,This scripting stuff is funVery true! Sorry I got it the wrong way round - you know where we are if you need a pointer or two to get it working the right way. 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
MrVining Posted February 23, 2013 Author Posted February 23, 2013 (edited) Is there a way to start at the end of the string working backwards? StringRegExpReplace('$string', ',', '', '1') To take out the last comma? Edited February 23, 2013 by MrVining
kylomas Posted February 23, 2013 Posted February 23, 2013 One of many ways to do that $str = 'some text followed by a comma,' if stringright($str,1) = ',' then $str = stringtrimright($str,1) ConsoleWrite($str & @LF) kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
MrVining Posted February 23, 2013 Author Posted February 23, 2013 NICE!!!! I figured out the StringTrimRight, but I didn't think to add an if!!! Thank you thank you!
Developers Jos Posted February 23, 2013 Developers Posted February 23, 2013 Do you not have to use EndIf?why? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
MrVining Posted February 23, 2013 Author Posted February 23, 2013 No one ever seems to use it on any of the examples or posts? Just wondering what best practice is.
Developers Jos Posted February 23, 2013 Developers Posted February 23, 2013 The helpfile explains the options for IF quite well. There are 2 options: A one line if the performs the task defined after the "Then", or the format where the If performs anything between the IF and the endIf line. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
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