iiyama Posted February 22, 2010 Posted February 22, 2010 Hi, I'm hoping that someone could give me a few tips on a problem I have regarding Arrays. What I'm wanting to do is load a txt file into an Array then search for some text and find the line number of said text. I then need to add an extra 1 to the line number it finds. So if it finds 5 then = 6, 11 = 12 and so on. I guess I would then be able to use the variable containg the "line number" to then copy a line from one text file into another. Example would be [Test1] 1=jshdjsjsj 2=jshsjdjdhsjdh [TESTa] <<<< find line number then add 1 to then put the 1=jsjshdfawe "line number" into a variable. 1=jsjshdfawe 2=363632 *** I would rather be able to copy 1=jsjshdfawe into a variable and not worry about adding a 1 to a number. Otherwise if it were possible then just be able to copy 1=jsjshdfawe into a variable instead for [Testa]. I can normaly work out how to do things but Arrays do confuse me a bit. I have attached what I have played with so far but can't think how to proceed. I have searched the forums but not been able to work out how to be able to do it. I hope someoneone would be be able to help me. I have only been able to capture the line of the search string. #include <Array.au3> #include <file.au3> Dim $aRecords _FileReadToArray("c:\in\test.txt",$aRecords) $sSearch = "[TESTa]" ;<== search text $iIndex = _ArraySearch($aRecords, $sSearch, 0, 0, 0, 1) If @error Then If $iIndex = -1 Then Exit EndIf Else MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".") _FileWriteToLine("c:\in\NEWfile.txt", 1, '11 = ' & $iIndex, 1) EndIf
Moderators Melba23 Posted February 22, 2010 Moderators Posted February 22, 2010 iiyama,Welcome to the AutoIt forum. You need to add 1 to the index you find to get the next line - like this:#include <Array.au3> #include <file.au3> ; Simulate loading file Global $aRecords[4] = [3, "[TESTa]", "1=jsjshdfawe", "2=363632"] ;_FileReadToArray("c:\in\test.txt", $aRecords) $sSearch = "[TESTa]" ;<== search text $iIndex = _ArraySearch($aRecords, $sSearch);, 0, 0, 0, 1) ; Not found If $iIndex = -1 Then Exit Else MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".") MsgBox(0, "WriteLine", $aRecords[$iIndex + 1]) ;_FileWriteToLine("c:\in\NEWfile.txt", 1, '11 = ' & $iIndex, 1) EndIfAsk if anything is unclear. There ia a good tutorial on arrays in the Wiki - you can find it here.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
iiyama Posted February 22, 2010 Author Posted February 22, 2010 Hi M23, Thanks for the quick reply and welcome. I just had a look at your reply and it worked a treat. I just needed to change a few things to my previous code that I got from yours and it was all good. I should now be able to use it for other things in the future. It's nice to be able to kinda get my head around Arrays as normaly I try and code any other way to avoid them. Thanks again.
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