How to simplify the code.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

How to simplify the code.

Post by samjith » Wed Jun 24, 2015 9:06 am

Hi,

I need to replace certain words in file. but i don't want to replaces the same words which are inside dollar symbol.


eg:- This is only an example $only$ ok? Here i want to replace "only" with "something".

current output:- This is something an example $something$ ok?
Required output:- This is something an example $only$ ok?.

Now iam using the following code. but it is very complicated code and it required lot of time.

Code: Select all

put the text of field "MytextField" into myfile
   put replaceText(myfile,"\n","^EOL") into myfile
   
   repeat with i = 1 to the number of chars in myfile
      if char i of myfile contains "$" then
         add 1 to tmp
         if tmp = 1 then
            put "^SOE" & CR & char i of myfile after yy
         else
            put char i of myfile & CR & "^EOE" after yy
            put 0 into tmp
         end if
      else
         put char i of myfile after yy
      end if
   end repeat

   
   put 0 into m
   repeat for each lines iT in yy
      if iT contains "$" then
         add 1 to m
         put " *****In_Line_Equation " & m & "***** " into so
         replace IT with so in myfile
         put  iT & cr after ELine
      end if     
   end repeat
   
   
   
   put replaceText(myfile, "\^SOE\n","") into myfile
   put replaceText(myfile, "\n\^EOE","") into myfile
   put replaceText(myfile, "\^EOL",cr) into myfile
   



Is there any idea to simplify the code. :P


Thanks,

Samjith.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to simplify the code.

Post by Thierry » Wed Jun 24, 2015 11:24 am

samjith wrote: current output:- This is something an example $something$ ok?
Required output:- This is something an example $only$ ok?.

Is there any idea to simplify the code.
Hi Samjith,

Here is a very simple one:

Code: Select all

on mouseUp
   put "This is something an example $something$ ok?" into T
   -- Required output:- This is something an example $only$ ok?.
   put replaceText( T, "\$something\$", "$only$" )
end mouseUp
What more do you need ?

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Re: How to simplify the code.

Post by samjith » Wed Jun 24, 2015 11:59 am

Hi,

I just said an example ("only"). Actually i don't know the exact word inside the dollar symbol. It may be one word or two word, some times it may be more than two lines.


eg:- Please check $This is only
an example$ like that.

Any idea guru :oops:

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to simplify the code.

Post by Thierry » Wed Jun 24, 2015 12:07 pm

samjith wrote:Hi,
Actually i don't know the exact word inside the dollar symbol.
It may be one word or two word, some times it may be more than two lines.
Ok, then what about this one before going further?

Code: Select all

on mouseUp
   put "This is something an example $something$ ok?" into T
   put "zxczxcxzc zxcxcsdsdssa $aaaa" &cr  after T
   put "bbbbb$ rtretrtetert" after T
   put replaceText( T, "(?ms)\$[^\$]+\$", "REPLACE_STRING" )
end mouseUp
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Re: How to simplify the code.

Post by samjith » Wed Jun 24, 2015 12:39 pm

Hi Thierry,

So it replace all the chunks inside dollar with "Replace_string".

Then my output will be a mistake.

I want the text inside the dollar without any harm.


Thanks,

Samjith.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to simplify the code.

Post by Thierry » Wed Jun 24, 2015 1:01 pm

samjith wrote: So it replace all the chunks inside dollar with "Replace_string".
Then my output will be a mistake.
Yes, I know you *don't* want this.
As I said, it's a start.

The 1st question is; does it catch all the text you need to?
I want the text inside the dollar without any harm.
I still don't get it :(
What do you want as a replacement?

Please, give enough details about your context/data/text
and the output you want.

Best,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

samjith
Posts: 85
Joined: Tue Mar 31, 2015 10:09 am

Re: How to simplify the code.

Post by samjith » Wed Jun 24, 2015 1:16 pm

Yes. But that regular expression wonder me. Thanks


Actually my need is that, I want to color (hyphenated,mdash,ndash) words.
While coloring, the math part inside the dollar also get colored.


eg:- Example code predicted rate of Co-localization $2-3$ redicted rate of Co--localization $s_{x}-d$

I want to color (hyphenated,mdash,ndash; here Co-localization) word. but i dont want to color (2-3, etc).


Thanks
Samjith.

Post Reply