Jump to content

Delete a COMPLETE line/string and Replace


Recommended Posts

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 by MariusN
Link to comment
Share on other sites

  • Moderators

MariusN,

Put your to-be-replaced and replacement lines into an array and then loop through it within your function. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

MariusN,

Put your to-be-replaced and replacement lines into an array and then loop through it within your function. :unsure:

M23

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 :-(
Link to comment
Share on other sites

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 :unsure:
Link to comment
Share on other sites

  • Moderators

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. :unsure:

Here is a short demo of how looping through an array might work for you:

#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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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:

#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! :D

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 :unsure:
Link to comment
Share on other sites

  • Moderators

MariusN,

My pleasure - and do come back and ask if anything is unclear even after your head is "in gear"! :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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 :unsure:

Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

  • Moderators

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. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...