| View previous topic :: View next topic |
| Author |
Message |
Tommy Admin Team
Joined: 16 Nov 2002 Posts: 746 Location: The Netherlands
|
Posted: Fri Aug 23, 2002 9:45 pm Post subject: |
|
|
I must say Prakash's technique of string replacement is brilliant I think the following slightly
modified code may run somewhat faster:
| 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
%%pos = @pos("'",%%word)
if @greater(%%pos,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,1))
%%fullword = %%fullword@substr(%%word,1,@pred(%%pos))"\'"
else
%%fullword = %%fullword"\'"
end
%%word = @strdel(%%word,1,%%pos)
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,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)
|
What I've done, is replacing the many instances of the function "@pos("'",%%word)"
with a %%pos variable, so that the function is only called once.
I think something that may complicate this problem, is that backslashes should also be
replaced, so that "\" becomes "\\", in order to allow for real backslashes to be used in
PHP... I couldn't solve this myself so far...
Tommy |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1566
|
Posted: Fri Aug 23, 2002 10:53 pm Post subject: |
|
|
Here you go, I changed the code slighly so now it allows you to call the whole search/replace as a routine via a GOSUB statement and VARIABLE. It also changes all \ to \\.
| Code: |
DIALOG CREATE,New Dialog,-1,0,578,281
DIALOG ADD,LIST,LIST1,18,11,542,244
DIALOG SHOW
list add,list1,"'hi"
list add,list1,
list add,list1,"can't don't wasn't \ shouldn't"
list add,list1,"aren't wasn't didn't"
list add,list1,"jimmy's \ julie's jim's"
list add,list1,"can't don't \\wasn't shouldn't"
list add,list1,"aren't w a s n ' t didn't"
list add,list1," jimmy's julie's jim's "
list add,list1,"yeah we'll \test the end '"
list add,list1,"'"
list add,list1,"\5 in a row - '''''"
rem Set the %%SearchReplaceVar holds two sets of chacters, the character you
rem want replaced then | then the actual new characters you want, for example
rem I use > '|\', here it replaces ' with \'.
%%SearchReplaceVar = "\|\\"
gosub searchreplaceroutine
%%SearchReplaceVar = "'|\'"
gosub searchreplaceroutine
wait event
:searchreplaceroutine
option fieldsep,|
list create,1
list create,2
parse "%%BeforeChar;%%AfterChar",%%SearchReplaceVar
rem HERE ASSIGN FROM THE LISTBOX YOU WANT TO READ FROM, I'M USING LIST1
rem IN THIS EXAMPLE AND ASSIGNING IT TO 1.
LIST ASSIGN,1,LIST1
%%index = 0
repeat
%%word = @item(1,%%index)
%%fullword = @trim()
repeat
%%pos = @pos(%%BeforeChar,%%word)
if @greater(%%pos,0)
if @not(@equal(%%pos,1))
%%fullword = %%fullword@substr(%%word,1,@pred(%%pos))%%AfterChar
else
%%fullword = %%fullword%%AfterChar
end
%%word = @strdel(%%word,1,%%pos)
end
until @equal(%%pos,0)
%%fullword = %%fullword%%word
list add,2,%%fullword
%%index = @succ(%%index)
until @equal(%%index,@count(1))
rem HERE ASSIGN FROM THE LISTBOX YOU WANT TO READ FROM, I'M USING 2
rem IN THIS EXAMPLE AND ASSIGNING IT BACK TO LIST1.
LIST ASSIGN,LIST1,2
list close,2
list close,1
exit
:close
exit
|
|
|
| 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
|
|