| View previous topic :: View next topic |
| Author |
Message |
VisorSoft Guest
|
Posted: Mon Aug 12, 2002 4:24 pm Post subject: Search and Replace Text |
|
|
I have tried to solve this problem only to not be able to find the answer.
How would I search for the character ' in a list and replace it with \'?
Thank you for any help.  |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1566
|
Posted: Mon Aug 12, 2002 4:55 pm Post subject: |
|
|
Hi there, this isn't the prettiest code, but I threw it together in 10 mins, you could of course use the @pos() function to make this faster and search for the ' character and then replace it but the code below will check each character (not as optimized but still works well):
| Code: |
list create,1
list add,1,hi
list add,1,
list add,1,can't don't wasn't shouldn't
list add,1,aren't wasn't didn't
list add,1,jimmy's julie's jim's
list add,1,can't don't wasn't shouldn't
list add,1,aren't wasn't didn't
list add,1,jimmy's julie's jim's
rem list 2 will hold all the data, if you want to replace
rem the contents of list1 instead, just use list insert commands
rem to insert the newly formated %%WORD into the list.
list create,2
rem here is the actual code, sets the first list index then goes through
rem each item line by line, and character by character.
%%index = 0
repeat
%%word = @item(1,%%index)
if @greater(@len(%%word),0)
%%wordpos = 1
repeat
if @equal(@substr(%%word,%%wordpos),"'")
%%word = @strdel(%%word,%%wordpos)
%%word = @strins(%%word,%%wordpos,"\'")
rem @succ() below was added because new inserted text is 2 chars in
rem length, we have to increment by 2 to skip both new chars
%%wordpos = @succ(%%wordpos)
end
%%wordpos = @succ(%%wordpos)
until @greater(%%wordpos,@len(%%word))
end
rem ok we replaced all "'" characters with "\'" the %%word var holds
rem the newly formated line, here I set it to a new listbox
list add,2,%%word
%%index = @succ(%%index)
until @greater(%%index,@count(1))
rem all done, just show you the newly formatted text which is in list 2
warn @text(2)
|
|
|
| Back to top |
|
 |
VisorSoft Guest
|
Posted: Mon Aug 12, 2002 5:08 pm Post subject: |
|
|
Thank you! This really helped a lot!
Thanks!  |
|
| Back to top |
|
 |
Que Valued Newbie

Joined: 25 Aug 2001 Posts: 38 Location: Newaygo, MICH
|
Posted: Tue Aug 13, 2002 2:54 pm Post subject: |
|
|
here is a repeat i wrote that replaced *s with a space in each line of a list.
| Code: |
%%cnt = 0
%%lines = @count(1)
repeat
%Y = @item(1,%%cnt)
%P = @pos(*,%Y)
if @zero(%P)
%%cnt = @succ(%%cnt)
else
%Y = @strins(%Y,%P," ")
%P = @pos(*,%Y)
%Y = @strdel(%Y,%P)
list put,1,%Y
end
until @equal(%%cnt,%%lines)
|
It will look through each line repeatedly until %P (the position of the *) is zero.
Note that I'm only using one list. That way we only write the lines we need to change as opposed to using two lists, and adding each line (changed or not) to the new list. _________________ Que |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1566
|
Posted: Tue Aug 13, 2002 3:05 pm Post subject: |
|
|
| Hi Que, without some changes to your code it will not work in his case. He needs to replace the ' character with \', with your code it would keep finding the initial instance of ' and replace it with \' again and again. |
|
| Back to top |
|
 |
Que Valued Newbie

Joined: 25 Aug 2001 Posts: 38 Location: Newaygo, MICH
|
Posted: Tue Aug 13, 2002 3:42 pm Post subject: |
|
|
I always struggle a little with counting my variables, but I think the code is good except for my variable increment at the end which was correct in my original source, but which I had changed here because it seemed wrong I hate it when I second guess myself .
Anyway here is a working script - no error checking on the file open.
| Code: |
Title Search & Replace
DIALOG CREATE,S&R,-1,0,220,100
DIALOG ADD,BUTTON,BUTTON1,50,35,70,24,Open File
DIALOG ADD,BUTTON,BUTTON2,50,125,70,24,Process File
dialog add,status,sb1
DIALOG SHOW
dialog disable,button2
dialog set,sb1,Click the Open Button
:Evloop
wait event
goto @event()
:BUTTON1BUTTON
%F = @filedlg("Text file (*.*)|*.*",Open file)
dialog enable,button2
Dialog set,sb1,Selected @path(%F)@name(%F)out.@ext(%F)
goto evloop
:BUTTON2BUTTON
list create,1
list loadfile,1,%f
%%cnt = 0
%%lines = @count(1)
repeat
%Y = @item(1,%%cnt)
%P = @pos(',%Y)
if @zero(%P)
%%cnt = @succ(%%cnt)
else
%Y = @strins(%Y,%P,/)
%P = @pos(',%Y)
%Y = @strdel(%Y,%P)
list put,1,%Y
end
until @equal(%%cnt,@succ(%%lines))
list savefile,1,@path(%F)@name(%F)out.@ext(%F)
dialog disable,button2
Dialog set,sb1,Saved @path(%F)@name(%F)out.@ext(%F)
goto evloop
:Close
exit
|
_________________ Que |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1566
|
Posted: Tue Aug 13, 2002 4:39 pm Post subject: |
|
|
Hi Que, your code replaces ' with just a \, Visorsoft needed it to be: \'.
 |
|
| Back to top |
|
 |
Que Valued Newbie

Joined: 25 Aug 2001 Posts: 38 Location: Newaygo, MICH
|
Posted: Tue Aug 13, 2002 5:18 pm Post subject: |
|
|
So much for attention to detail... You're right, my code only works for single char replacements... I didn't notice we needed to replace one char with two.  _________________ Que
Last edited by Que on Wed Aug 14, 2002 12:30 pm; edited 1 time in total |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Aug 22, 2002 8:28 pm Post subject: |
|
|
Hi VisorSoft,
I was wondering if you had found a way to make that replace technique faster?
PGWARE had mentioned @pos()...but I can't figure out how to use it.  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Thu Aug 22, 2002 9:13 pm Post subject: |
|
|
Did ya check the link I posted?
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Thu Aug 22, 2002 10:14 pm Post subject: |
|
|
I missed that.
Sorry...  _________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Danny Young Newbie

Joined: 16 Aug 2002 Posts: 4 Location: Manchester, England
|
Posted: Fri Aug 23, 2002 12:13 am Post subject: |
|
|
To be honest PGWARE, your way is probably more efficient than using @POS().
I think you're counting from 1 to the length of the string, checking each char, but at least you only pass each char once, whereas repeatedly using @POS() you're searching from the earlier chars again and again...
Least thats how I presume @POS() works. |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Fri Aug 23, 2002 1:32 am Post subject: |
|
|
It is true that @pos() starts from the beginning of the
string each time, but it's gonna usually be way faster
than cycling thru each char. One reason, is that it only
checks once per string if nothing is found. Also, it's a
VDS function, and will be faster than anything you can
actually build within VDS....
Cheers, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1566
|
Posted: Fri Aug 23, 2002 4:11 am Post subject: |
|
|
Ok here is the code via @POS routine, it does seem to be faster and I would recommend using this. No need to check each char when @pos does that for you - it's also a little redundant and will be slow with long lines the other way I did it.
| Code: |
list create,1
list add,1,"'hi"
list add,1,
list add,1,"can't don't wasn't shouldn't"
list add,1,"aren't wasn't didn't"
list add,1,"jimmy's julie's jim's"
list add,1,"can't don't wasn't shouldn't"
list add,1,"aren't w a s n ' t didn't"
list add,1," jimmy's julie's jim's "
list add,1,"yeah we'll test the end '"
list add,1,"'"
list add,1,"5 in a row - '''''"
rem list 2 will hold all the data, if you want to replace
rem the contents of list1 instead, just use list insert commands
rem to insert the newly formated %%FULLWORD into the list.
list create,2
rem here is the actual code, sets the first list index then goes through
rem each item line by line
%%index = 0
repeat
%%word = @item(1,%%index)
%%fullword = @trim()
repeat
if @greater(@pos("'",%%word),0)
rem we check the position of the ' character, we have to use an if/else
rem to see if the ' begins at the first character, if so we need to
rem use a different routine so the ' doesn't repeat itself
if @not(@equal(@pos("'",%%word),1))
%%fullword = %%fullword@substr(%%word,1,@pred(@pos("'",%%word)))"\'"
else
%%fullword = %%fullword"\'"
end
%%word = @strdel(%%word,1,@pos("'",%%word))
rem we check with @pos for any instance of ' and replace it with \'
rem then set it to a variable chunk by chunk till we get the whole
rem sentence.
end
until @equal(@pos("'",%%word),0)
rem ok we replaced all "'" characters with "\'" the %%fullword var holds
rem the newly formated line, here I set it to a new listbox
%%fullword = %%fullword%%word
list add,2,%%fullword
%%index = @succ(%%index)
until @equal(%%index,@count(1))
rem all done, just show you the newly formatted text which is in list 2
warn @text(2)
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|