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 

String Replace Percent Sign

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



Joined: 24 Jun 2008
Posts: 104

PostPosted: Wed Sep 17, 2008 2:32 am    Post subject: String Replace Percent Sign Reply with quote

I am trying to figure out how to do a string replace for the % symbol.

Example, I have a paragraph of text:

The student got 100% on the test. etc. etc. etc.

This text is in a text file, and I am doing a file read into the clipboard, and then doing a string replace, and appending it to a library of variables.

Example:

Code:
StringReplace, clipboard, clipboard, `%,```%, All   



This is what I have so far. But it's not seeming to replace the percent symbols like I want it to. When I open the library of variables, it wont let me because it only has one percent symbol and it is seeing it as an incomplete variable.

How would I go about doing this?

Thanks for your help in advance!
Back to top
View user's profile Send private message
Mustang



Joined: 17 May 2007
Posts: 421
Location: England

PostPosted: Wed Sep 17, 2008 3:17 am    Post subject: Reply with quote

Like this?
Code:
OriginalText := "The student got 100% on the test."
StringReplace, ReplacedText, OriginalText, `%, {PERCENT SIGN}, All
MsgBox, Original Text: %OriginalText%`nReplaced Text: %ReplacedText%


MsgBox wrote:
Original Text: The student got 100% on the test.
Replaced Text: The student got 100{PERCENT SIGN} on the test.
Back to top
View user's profile Send private message
smikkelsen



Joined: 24 Jun 2008
Posts: 104

PostPosted: Wed Sep 17, 2008 4:34 am    Post subject: Reply with quote

That seems to do the trick mustang!

Thanks
Back to top
View user's profile Send private message
smikkelsen



Joined: 24 Jun 2008
Posts: 104

PostPosted: Wed Sep 17, 2008 7:18 am    Post subject: Reply with quote

I Spoke too soon. all that is doing now is pasting the words {PERCENT SIGN} in my text when i call the variable.

EXAMPLE:
Code:

clipboard= %var%

send, ^V


Var containing the string that i did the replace on, doesn't put the actual percent sign in the text when i'm pasting it.

Am i doing something wrong?

Thanks again for responding.
Back to top
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Wed Sep 17, 2008 8:35 pm    Post subject: Reply with quote

Code:
OriginalText := "The student got 100% on the test."
StringReplace, ReplacedText, OriginalText, `%, `%, All
MsgBox, Original Text: %OriginalText%`nReplaced Text: %ReplacedText%
Clipboard := ReplacedText
Send, ^v

_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
Mustang



Joined: 17 May 2007
Posts: 421
Location: England

PostPosted: Fri Sep 19, 2008 12:43 am    Post subject: Reply with quote

It was just an example
If you need more help I suggest providing a better explanation of what you are trying to achieve
Or even post what code you have so far
Back to top
View user's profile Send private message
smikkelsen



Joined: 24 Jun 2008
Posts: 104

PostPosted: Fri Sep 19, 2008 5:21 am    Post subject: Reply with quote

I thought I gave as much info as needed, but I will admit, this probabl isn't a common use. Here is the majority of the code, I cut out a whole bunch of other string replaces that aren't relevent to the problem.

Code:

#include, functions.ahk


FileSelectFile, files1, , %openfile%    ; here I am selecting a .pdf file
StringTrimRight, files2, files1, 3       ; here it takes the pdf off the end of the file name
files=%files2%txt                           ; it is replacing the pdf with txt so it can save it later using this name


if files =
   {
    MsgBox, The user pressed cancel.
    return
}

   
    Run, %files1%                             ; run the selected pdf file
   sleep, 6000
    msgbox, make sure PDF is open.
    sleep, 1000
    send, {click}
    send, {alt down}fv{alt up}
   
    sleep, 2000
    clipboard=%files%
    send, ^v
    sleep, 200
    send, {enter}                             ; all of that saves it as a text file.  (i wish there were an easier way)
   
    sleep, 3000
    FileRead, filecont, %files%               ; it is now reading the txt version of the originally selected pdf into the clipboard
    clipboard = %filecont%     
   



StringReplace, clipboard, clipboard, `%, ```%, All          ;here, it should find all % signes and replace them with `%




FileAppend,                         ; it now appends this information to a temporary library (the library includes nothing but variables produced by the txt file.  a series of string replaces turns the text into variables for me)
(
%clipboard%
), %A_ScriptDir%\temp.ahk

FileRecycle, %files%
run, gc2.ahk                         ; this now runs a different script which contains the #include, temp.ahk

Return

   



StringReplace, clipboard, clipboard, `%, ```%, All




FileAppend,
(
%clipboard%
), %A_ScriptDir%\temp.ahk

FileRecycle, %files%
run, gc2.ahk

Return


Now, the problem lies within the temp.ahk. Here is what happens, if the txt file contains a percent sign, and it gets saved as a variable in the temp.ahk then when i go to use the temp.ahk library, it gives me the error because of the percent sign.

Example:
Code:

var= the student got 100 % on his test.


if you have a library containing this variable, it wont let you run it, or include it because the % symbol is not escaped.

This is what it needs to look like:
Code:

var= the student got 100 `% on his test.


So, how do you get it from the clipboard as: 100% to the library that needs to be run later as: 100`% ?

I hope that makes more sense.

Thanks again for the help.
Back to top
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1390
Location: The Interwebs

PostPosted: Fri Sep 19, 2008 5:26 am    Post subject: Reply with quote

So you want to replace all % with `%?

If so, then
Code:
StringReplace, clipboard, clipboard, `%,```%, All
does exactly that. And yes, I have tested it.

Try simply:
Code:
Clipboard := "This % is %% something % %"
StringReplace, clipboard, clipboard, `%,```%, All
MsgBox % Clipboard
Back to top
View user's profile Send private message AIM Address
smikkelsen



Joined: 24 Jun 2008
Posts: 104

PostPosted: Fri Sep 19, 2008 6:32 am    Post subject: Reply with quote

I hoped that was the correct way to do it, and I have tested it to on a simple notepad, or msgbox test, but when it comes to running my full script, for some reason, it misses them. I'm not sure if it's missing all or some, but it is missing them on occasion. Is there something else that would be causing that? Or can string replace be sometimes undependable? I can't figure out what the problem is if that is the one and only correct way to replace it.

Thank you all for your responses.
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 2214
Location: switzerland

PostPosted: Fri Sep 19, 2008 12:43 pm    Post subject: Reply with quote

just a script saves marked text to a texfile (when press key F12)
Code:
F12::
F1=%A_NOW%.txt
P1=%A_Desktop%\   
transform,S,chr,37
transform,E,chr,96

clipboard=
Send ^c
Sleep,100
cl:=clipboard
StringReplace,cl,cl,`%,%E%%S%,All
FileAppend,%cl%`r`n,%P1%\%F1%
Splashimage,,b w600 h150 x100 Y400 CWsilver m9 b fs10 zh0,SAVE`n%cl%
sleep,900
Splashimage, off
return
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