Jump to content

Replace partial string in array


shornw
 Share

Recommended Posts

I have to make changes to about 1000 .txt files, and would like to do it using an array, as I have always struggled with any array functions other than simple stuff.

1. remove blank lines

2. edit a string within one of the lines

I have removed all the blank lines quite easily -

For $i = UBound($aArr1) - 1 To 0 Step -1
   if stringlen($aArr1[$i]) = 0 Then _ArrayDelete($aArr1, $i)
Next

The edit string withn the array is proving more 'challenging'. the array entry is along the lines of 'Smart Array 6i in Slot 0 (Embedded)' (typically it could be different and is definitely in a different line position in some files) but I want to amend the 'slot' to 'slot='. I know I can use _ArrayToString(), StringInString() and StringReplace(), and I have experimented with:

_ArrayToClip($aArr1,1)
ClipPut(StringReplace(ClipGet(), "slot", "slot="))

which works, as in it replaces the string, but how do I get the modified string or clipboard contents back into the array, or do I have to create a second array and write it to that.

Is therre an opportunity for a _ArrayReplace() function (better minds than mine will answer this)

Thanks for any help anyone can offer

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Try this:

#include <Array.au3>
$aTxt = StringRegExp(FileRead("Test.reg"), "(.+)\s+", 3) ;remove all empty lines
_ArrayDisplay($aTxt)
$search = "HKEY_LOCAL_MACHINE"
$replace = "HKLM"
$aIndices = _ArrayFindAll($aTxt, $search, 0, 0, 0, 1)
For $i = 0 To UBound($aIndices) - 1
$aTxt[$aIndices[$i]] = StringReplace($aTxt[$aIndices[$i]], $search, $replace) ;replace HKEY_LOCAL_MACHINE string with HKLM
Next
_ArrayDisplay($aTxt)

Test.reg is a registry file with some empty lines.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks for the replies guys, I knew I could achieve it without using an array, but I wanted to make sure that I hadn't missed something, and also use it to develop array knowledge.

UEZ - can you explain how

$aTxt = StringRegExp(FileRead("Test.reg"), "(.+)\s+", 3)
replaces blank lines ie what do "(.+)\s+" represent.

Thanks

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Well, regular expression is neither easy to explain and nor to understand. I'm still in learning mode.

(.+)\s+ means group all character until any whitespace character. Only the grouped characters will be listed in the array.

GEOSoft can explain it much better than me.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

WHOA!!!! I've never touched RegEXP, and I think I'm glad I haven't had to. This is a whole new learning experience for me. It's a bit like learning an Asiatic language - there are no recognisable reference points. I can see the use tho, but I'm gonna need some time to get my head round it.

Thanks for advice, especially to UEZ. As always, I'm ubah impressed by the knowledge and helpfulness of forum members.

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

UEZ - can you explain how

$aTxt = StringRegExp(FileRead("Test.reg"), "(.+)\s+", 3)
replaces blank lines ie what do "(.+)\s+" represent.

Just for the reference

here is derived script which remove empty lines and returns string instead of array

$Txt = StringRegExpReplace(FileRead("Test.reg"), "(.+)\s+", '\1') ; remove all empty lines
Edited by Zedna
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...