MariusN Posted May 22, 2011 Share Posted May 22, 2011 (edited) I am trying to replace strings, but only manage to change ONE string/line in a file...I would like to do this function, but with about 20 lines that need to be changed in a file The program basicaly SEEKS for a string, then DLETES the COMPLETE line and OVERWRITE it with a completely new string. How can i make this search for the remainung 19 lines? here is the code ---> #include <file.au3> #include <Array.au3> Opt("MustDeclareVars", 1) Global $_Array , $file, $pref1, $pref2, $_Element, $replace1, $replace2 $file = "all.js" $pref1 = '"network.http.keep-alive.timeout"' <---FIRST SEARCH $replace1 = 'pref("network.http.keep-alive.timeout", 90)' <---THIS LINE NEEDS TO REPLACE THE PRIOR $pref2 = '"network.http.max-connections"'<---SECOND SEARCH $replace2 = 'pref("network.http.max-connections", 20)'<---THIS LINE NEEDS TO REPLACE THE PRIOR _FileReadToArray($file, $_Array) $_Array = _DeleteArrayElementWithStringInstr($_Array,$pref1) _FileWriteFromArray($file, $_Array, 1) Exit Func _DeleteArrayElementWithStringInstr($_Array, $pref1) Local $_Item For $_Element In $_Array If StringInStr($_Element, $pref1) <> 0 Then _ArrayDelete($_Array, $_Item) _ArrayInsert($_Array, $_Item, $replace1) <---THIS WORKS 100%, BUT I NEED IT TO "LOOP" TO --->$replace2 Else $_Item += 1 EndIf Next Return ($_Array) EndFunc ;==>_DeleteArrayElementWithStringInstr Edited May 22, 2011 by MariusN Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 22, 2011 Moderators Share Posted May 22, 2011 MariusN, Put your to-be-replaced and replacement lines into an array and then loop through it within your function. 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...
MariusN Posted May 22, 2011 Author Share Posted May 22, 2011 MariusN,Put your to-be-replaced and replacement lines into an array and then loop through it within your function. M23Let me be truely honest with you Melba23...i know ZILTS about arrays...really. I have tried numourus times but somehow "lack" that bit of knowlege. You cant maybe give me a small "example"? Anything else, but that has nothing to do with THIS paticular code....just so i can get the "hang" of it...please my friend :-( Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted May 22, 2011 Share Posted May 22, 2011 Let me be truely honest with you Melba23...i know ZILTS about arrays...really. I have tried numourus times but somehow "lack" that bit of knowlege. You cant maybe give me a small "example"? Anything else, but that has nothing to do with THIS paticular code....just so i can get the "hang" of it...please my friend :-(There's a Array Tutorial in the wiki .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
MariusN Posted May 22, 2011 Author Share Posted May 22, 2011 There's a Array Tutorial in the wiki thx admiralAlkex....maybe i'm just a little "rusted" Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 22, 2011 Moderators Share Posted May 22, 2011 MariusN, You really do need to learn about arrays - they are a fundamental part of coding. I was going to recommend the Wiki tutorial myself but I see AdmiralAlkex has already provided the link to it. Here is a short demo of how looping through an array might work for you: expandcollapse popup#include <file.au3> #include <Array.au3> Opt("MustDeclareVars", 1) Global $_Array , $file, $pref1, $pref2, $_Element, $replace1, $replace2, $aReplace[4] $file = "all.js" ; Create the array with the to-be-replaced and replacement lines in alternate elements $aReplace[0] = '"network.http.keep-alive.timeout"' ;<---FIRST SEARCH - even element $aReplace[1] = 'pref("network.http.keep-alive.timeout", 90)' ;<---THIS LINE NEEDS TO REPLACE THE PRIOR - odd element $aReplace[2] = '"network.http.max-connections"' ;<---SECOND SEARCH - even element $aReplace[3] = 'pref("network.http.max-connections", 20)' ;<---THIS LINE NEEDS TO REPLACE THE PRIOR - odd element ;_FileReadToArray($file, $_Array) ; Simulate the file being read into an array Global $_Array[3] = [2, '"network.http.keep-alive.timeout"', '"network.http.max-connections"'] ; Do the business $_Array = _DeleteArrayElementWithStringInstr($_Array,$aReplace) ; And show the result _ArrayDisplay($_Array) ;_FileWriteFromArray($file, $_Array, 1) Exit Func _DeleteArrayElementWithStringInstr($_Array, $aReplace) Local $iIndex ; Loop through the array - Step 2 means that we only look at every second line which are the ones with the to-be-replaced lines For $i = 0 To UBound($aReplace) - 1 Step 2 ; Get the index of a matching line $iIndex = _ArraySearch($_Array, $aReplace[$i]) ; if we found a valid line If $iIndex <> -1 Then ; Then replace the line with the replacement line from the array $_Array[$iIndex] = $aReplace[$i + 1] EndIf Next Return $_Array EndFunc ;==>_DeleteArrayElementWithStringInstr I have deliberately kept it as simple as I can - I would code it with a 2D array myself, but let us walk before we start running! Does it make sense? Please ask if not. 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...
MariusN Posted May 22, 2011 Author Share Posted May 22, 2011 MariusN, You really do need to learn about arrays - they are a fundamental part of coding. I was going to recommend the Wiki tutorial myself but I see AdmiralAlkex has already provided the link to it. Here is a short demo of how looping through an array might work for you: expandcollapse popup#include <file.au3> #include <Array.au3> Opt("MustDeclareVars", 1) Global $_Array , $file, $pref1, $pref2, $_Element, $replace1, $replace2, $aReplace[4] $file = "all.js" ; Create the array with the to-be-replaced and replacement lines in alternate elements $aReplace[0] = '"network.http.keep-alive.timeout"' ;<---FIRST SEARCH - even element $aReplace[1] = 'pref("network.http.keep-alive.timeout", 90)' ;<---THIS LINE NEEDS TO REPLACE THE PRIOR - odd element $aReplace[2] = '"network.http.max-connections"' ;<---SECOND SEARCH - even element $aReplace[3] = 'pref("network.http.max-connections", 20)' ;<---THIS LINE NEEDS TO REPLACE THE PRIOR - odd element ;_FileReadToArray($file, $_Array) ; Simulate the file being read into an array Global $_Array[3] = [2, '"network.http.keep-alive.timeout"', '"network.http.max-connections"'] ; Do the business $_Array = _DeleteArrayElementWithStringInstr($_Array,$aReplace) ; And show the result _ArrayDisplay($_Array) ;_FileWriteFromArray($file, $_Array, 1) Exit Func _DeleteArrayElementWithStringInstr($_Array, $aReplace) Local $iIndex ; Loop through the array - Step 2 means that we only look at every second line which are the ones with the to-be-replaced lines For $i = 0 To UBound($aReplace) - 1 Step 2 ; Get the index of a matching line $iIndex = _ArraySearch($_Array, $aReplace[$i]) ; if we found a valid line If $iIndex <> -1 Then ; Then replace the line with the replacement line from the array $_Array[$iIndex] = $aReplace[$i + 1] EndIf Next Return $_Array EndFunc ;==>_DeleteArrayElementWithStringInstr I have deliberately kept it as simple as I can - I would code it with a 2D array myself, but let us walk before we start running! Does it make sense? Please ask if not. M23 Thanks Melba...youre a star....i did a couple of "arrays" in the past, but VERY few...and a very long time ago (as i'm sure uou have noticed), but i wont use it as an excuse ...Thanks a lot...now time to do the "digging" and get my head in gear...thx once again Melba Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 22, 2011 Moderators Share Posted May 22, 2011 MariusN,My pleasure - and do come back and ask if anything is unclear even after your head is "in gear"! 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...
MariusN Posted May 22, 2011 Author Share Posted May 22, 2011 MariusN,My pleasure - and do come back and ask if anything is unclear even after your head is "in gear"! M23...your stuff ALWAYS makes sense...thats why i remember years ago i still asked you WHY you havnt become one of the ADMINS here....remember? Anyways....thx Melba Link to comment Share on other sites More sharing options...
jchd Posted May 22, 2011 Share Posted May 22, 2011 Melba for President! I second that, as he flies well above us. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 22, 2011 Moderators Share Posted May 22, 2011 jchd, Who needs to be a president? The grins on the faces of the 8 cadets I took for their first flights yesterday is enough for me. 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...
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