AutoHotkey Community

It is currently May 26th, 2012, 12:23 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: September 17th, 2008, 3:32 am 
Offline

Joined: June 24th, 2008, 8:30 am
Posts: 126
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 4:17 am 
Offline

Joined: May 17th, 2007, 9:06 pm
Posts: 421
Location: England
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 5:34 am 
Offline

Joined: June 24th, 2008, 8:30 am
Posts: 126
That seems to do the trick mustang!

Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 8:18 am 
Offline

Joined: June 24th, 2008, 8:30 am
Posts: 126
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 9:35 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 1:43 am 
Offline

Joined: May 17th, 2007, 9:06 pm
Posts: 421
Location: England
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 6:21 am 
Offline

Joined: June 24th, 2008, 8:30 am
Posts: 126
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 6:26 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 7:32 am 
Offline

Joined: June 24th, 2008, 8:30 am
Posts: 126
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 1:43 pm 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
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


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Morpheus, RUBn, SKAN, sks, Yahoo [Bot] and 23 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