AutoHotkey Community

It is currently May 27th, 2012, 1:33 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: November 21st, 2009, 12:38 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 21st, 2009, 12:44 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 21st, 2009, 9:42 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 21st, 2009, 11:19 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 21st, 2009, 11:29 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Of course you could use ini to store info, with iniread/write you wouldn't mess up anything.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 22nd, 2009, 9:55 am 
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?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 22nd, 2009, 9:58 am 
OK, I got it: using #CommentFlag // in the source file and replacing all ; with //


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 22nd, 2009, 10:23 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 22nd, 2009, 10:27 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Use backtic to escapce it
Code:
StringReplace, str,str, `; comment, `; new comment, all

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Blackholyman, bobbysoon, iDrug, Ohnitiel, Tipsy3000, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group