forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Search and Replace Text
Goto page 1, 2  Next
 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
VisorSoft
Guest





PostPosted: Mon Aug 12, 2002 4:24 pm    Post subject: Search and Replace Text Reply with quote

I have tried to solve this problem only to not be able to find the answer. Sad

How would I search for the character ' in a list and replace it with \'?

Thank you for any help. Smile
Back to top
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1566

PostPosted: Mon Aug 12, 2002 4:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
VisorSoft
Guest





PostPosted: Mon Aug 12, 2002 5:08 pm    Post subject: Reply with quote

Thank you! This really helped a lot!


Thanks! Very Happy
Back to top
Que
Valued Newbie


Joined: 25 Aug 2001
Posts: 38
Location: Newaygo, MICH

PostPosted: Tue Aug 13, 2002 2:54 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1566

PostPosted: Tue Aug 13, 2002 3:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Que
Valued Newbie


Joined: 25 Aug 2001
Posts: 38
Location: Newaygo, MICH

PostPosted: Tue Aug 13, 2002 3:42 pm    Post subject: Reply with quote

I always struggle a little with counting my variables, Smile 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 Embarassed 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
View user's profile Send private message Send e-mail
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1566

PostPosted: Tue Aug 13, 2002 4:39 pm    Post subject: Reply with quote

Hi Que, your code replaces ' with just a \, Visorsoft needed it to be: \'.
Sad
Back to top
View user's profile Send private message
Que
Valued Newbie


Joined: 25 Aug 2001
Posts: 38
Location: Newaygo, MICH

PostPosted: Tue Aug 13, 2002 5:18 pm    Post subject: Reply with quote

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. Embarassed
_________________
Que


Last edited by Que on Wed Aug 14, 2002 12:30 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Tue Aug 13, 2002 8:35 pm    Post subject: Reply with quote

Here's a "replace text" program in the VDS archives:

http://www.vdsworld.com/archive/index.php?page=topic&board=3&topic=136

It also does some other text functions such as sort,
convert to upper case, lowercase, and find duplicates.

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
FreezingFire
Admin Team


Joined: 23 Jun 2002
Posts: 3508

PostPosted: Thu Aug 22, 2002 8:28 pm    Post subject: Reply with quote

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. Sad

_________________
FreezingFire
VDSWORLD.com
Site Admin Team
Back to top
View user's profile Send private message Visit poster's website
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Aug 22, 2002 9:13 pm    Post subject: Reply with quote

Did ya check the link I posted?

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
FreezingFire
Admin Team


Joined: 23 Jun 2002
Posts: 3508

PostPosted: Thu Aug 22, 2002 10:14 pm    Post subject: Reply with quote

I missed that. Crying or Very sad

Sorry... Embarassed

_________________
FreezingFire
VDSWORLD.com
Site Admin Team
Back to top
View user's profile Send private message Visit poster's website
Danny Young
Newbie


Joined: 16 Aug 2002
Posts: 4
Location: Manchester, England

PostPosted: Fri Aug 23, 2002 12:13 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website MSN Messenger
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Fri Aug 23, 2002 1:32 am    Post subject: Reply with quote

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 Smile

_________________
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
View user's profile Send private message Send e-mail
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1566

PostPosted: Fri Aug 23, 2002 4:11 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group