AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple problem with text replacement

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Nov 21, 2009 11:38 am    Post subject: Simple problem with text replacement Reply with quote

Very simple problem. I have a text file with this content:

joy1
joy2
joy10

And a script which shoud replace text according to the input in some input boxes. The problem comes with 1 and 10, because 1 is part of 10, so the text replacement is wrong in many cases. Any idea how to fix this?

inputbox, j1, Button configuration, Enter the number of your 1st button,,, 150
inputbox, j2, Button configuration, Enter the number of your 2nd button,,, 150
inputbox, j10, Button configuration, Enter the number of your 10th button,,, 150

FileRead, text, source.txt
StringReplace, text, text, joy1, joy%j1%, All
StringReplace, text, text, joy2, joy%j2%, All
StringReplace, text, text, joy10, joy%j10%, All
FileAppend, %text%, result.txt
Back to top
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Nov 21, 2009 11:44 am    Post subject: Reply with quote

Try adding `n
Code:
StringReplace, text, text, joy1`n, joy%j1%`n, All
StringReplace, text, text, joy2, joy%j2%, All
StringReplace, text, text, joy10`n, joy%j10%`n, All
or `r`n depending on the file. Of course there must be no spaces after joyX and the last line must be an empty line: _ is END OF FILE

OK:
Quote:
joy9
Joy10
_
Not OK:
Quote:
joy9
Joy10_
here the Joy10`n does not exist because _ is a end of file char, not `n

If you use regexreplace you can use ^ and $ mark beginning and end of line to make sure you don't mix 1 and 10 1$ 10$
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Nov 21, 2009 8:42 pm    Post subject: Reply with quote

I've fixed this problem because, since I'm replacing hotkeys, I can specify '::' after the text to be replaced. However, I've found that the problem persists, because the text replacements don't happen all at the same time, but one after one, so as the script replaces text, it can replace what has already been replaced, so the result is wrong most of the times. I wonder how I could find a workaround for this. This is my script and the source file (simplified for testing):


-----

joy1::
joy2::
joy3::
joy4::
joy5::
joy6::
joy7::
joy8::
joy9::
joy10::
joy11::

Code:

inputbox, j1, Button configuration, Enter the number of your 1st button,,310,130
inputbox, j2, Button configuration, Enter the number of your 2nd button,,310,130
inputbox, j3, Button configuration, Enter the number of your 3rd button,,310,130
inputbox, j4, Button configuration, Enter the number of your 4th button,,310,130
inputbox, j5, Button configuration, Enter the number of your 5th button,,310,130
inputbox, j6, Button configuration, Enter the number of your 6th button,,310,130
inputbox, j7, Button configuration, Enter the number of your coin button (player 1),,310,130
inputbox, j8, Button configuration, Enter the number of your coin button (player 2),,310,130
inputbox, j9, Button configuration, Enter the number of your start button,,310,130
inputbox, j10, Button configuration, Enter the number of your pause button,,310,130

FileRead, text, source.txt

StringReplace, text, text, joy4::, joy%j1%::, All
StringReplace, text, text, joy1::, joy%j2%::, All
StringReplace, text, text, joy8::, joy%j3%::, All
StringReplace, text, text, joy3::, joy%j4%::, All
StringReplace, text, text, joy2::, joy%j5%::, All
StringReplace, text, text, joy6::, joy%j6%::, All
StringReplace, text, text, joy9::, joy%j7%::, All
StringReplace, text, text, joy7::, joy%j8%::, All
StringReplace, text, text, joy5::, joy%j9%::, All 
StringReplace, text, text, joy10::, joy%j10%::, All

FileAppend, %text%, target.txt
Back to top
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Nov 21, 2009 10:19 pm    Post subject: Reply with quote

make them unique
Code:
StringReplace, text, text, joy4::, xoy%j1%::, All
StringReplace, text, text, joy1::, xoy%j2%::, All ; etc
StringReplace, text, text, joy8::, xoy%j3%::, All
StringReplace, text, text, joy3::, xoy%j4%::, All
StringReplace, text, text, joy2::, xoy%j5%::, All
StringReplace, text, text, joy6::, xoy%j6%::, All
StringReplace, text, text, joy9::, xoy%j7%::, All
StringReplace, text, text, joy7::, xoy%j8%::, All
StringReplace, text, text, joy5::, xoy%j9%::, All
StringReplace, text, text, joy10::, xoy%j10%::, All
StringReplace, text, text, xoy, joy, All ; and one more to fix back what we've just messed up above 
Wink
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sat Nov 21, 2009 10:29 pm    Post subject: Reply with quote

Of course you could use ini to store info, with iniread/write you wouldn't mess up anything.
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Guest






PostPosted: Sun Nov 22, 2009 8:55 am    Post subject: Reply with quote

Good idea. I'm trying to use comments in the source file to avoid this problem. However, stringreplace has problems with ; See:

StringReplace, text, text, ~1Joy2:: ; 5th button, ~1joy%j5%:: ; 5th button, All

Any idea how to fix that?
Back to top
Guest






PostPosted: Sun Nov 22, 2009 8:58 am    Post subject: Reply with quote

OK, I got it: using #CommentFlag // in the source file and replacing all ; with //
Back to top
rtcvb32



Joined: 17 Feb 2008
Posts: 289

PostPosted: Sun Nov 22, 2009 9:23 am    Post subject: Reply with quote

Anonymous wrote:
OK, I got it: using #CommentFlag // in the source file and replacing all ; with //


Actually i'd recommend you use /// or /* */ instead of //. Only reason, is if you ever want to use the // Integer divisor, you'd have a lot of issues.
Back to top
View user's profile Send private message Yahoo Messenger
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Sun Nov 22, 2009 9:27 am    Post subject: Reply with quote

Use backtic to escapce it
Code:
StringReplace, str,str, `; comment, `; new comment, all

_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group