How to get lines from braces.

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

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

How to get lines from braces.

Post by samjith » Tue Oct 13, 2015 9:34 am

Hi,

Is there any command to get lines inside a braces?
I need to get the lines inside curly braces. The lines may be 1,2....100 (it may be a paragraph in braces). Sometimes brace will with in braces.
I wish to create XML from Latex code.


For example:-

\good{This
is \bad{
an} example
to show
you}

i need :-
<good>This is <bad>an</bad> example to show you</good>

like that. Is it possible?. any idea :(


Thanks
--------------
Samjith

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: How to get lines from braces.

Post by dunbarx » Tue Oct 13, 2015 2:57 pm

Hi.

It is straightforward, but tedious, to deconstruct your example text into the format you want. But unless that original format is fixed, each instance would have to be handled individually. For example, just to begin, with your starting text in field 1, and a button with this in its script:

Code: Select all

on mouseUp
   get fld 1
   replace "\" with "<" in it
   replace return with space in it
   set the itemDel to "{"
   put  item 1 of it & ">" into temp
   
   put temp after accum
   put item 2 of it & ">" after accum
   
   answer accum
end mouseUp
You get the start of your desired output. But the deconstruction is intimately tied to the exact formatting of that text, and any variation will break a general solution. So the question again is this: will the original formatting always be the same? That is, will all the tags be arranged in the same order? I doubt it, no?

Craig Newman

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

Re: How to get lines from braces.

Post by samjith » Wed Oct 14, 2015 6:18 am

Hi,

All the tags will not be in the same order.
I know its very hard. but i have an idea,

Start from the bottom of the field (last word to first word), so i get the first from last open brace ("{"), and there must be a closing brace next after some words in the field.

Code: Select all

put the number of words of field "Field" into p
   repeat with i =  p down to 1
      if the word i of field "Field" contains "{" then
       --put replaceText(word i of fld "Field","\\[A-z,a-z]*(?ms)\{[^\}]+\}","good") into word i fld "Field"
         put replaceText(word i of fld "Field","(\\[A-z,a-z]*)((?ms)\{[^\}]+\})","\<\1\>\2\<\\\1\>") into word i fld "Field"
// i know this regularexpression will not work, i just want to show my requirement 
exit repeat
      end if 
   end repeat
Thanks
Samjith

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

Re: How to get lines from braces.

Post by samjith » Wed Oct 14, 2015 10:00 am

Hi,

Got something,

Code: Select all

on mouseUp
   lock screen
   put the number of words of field "Field" into p
   repeat with i =  p down to 1
      put 0 into tSkip
      repeat
         get wordOffset("}",field "Field",tSkip)
         if it = 0 then exit repeat
         add it to tSkip
      end repeat
      if matchText( the word i to p of field "Field", "(?ms)\\([A-Z,a-z]*)(?ms)\{([^\}]+)\}", tag,theValue) then
         put "<"&tag&">" &theValue&"</"&tag&">" into the word i to tSkip  of field "Field"
      end if 
   end repeat
   unlock screen
end mouseUp
But it is too slow. but it may be fast by using function
.
Can anybody help me to change this code to function :(

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

Re: How to get lines from braces.

Post by Thierry » Wed Oct 14, 2015 12:17 pm

Hi Samjith,

I didn't check your code but here is the 1st thing to do:

First line of your code, add:

Code: Select all

put field "Field" into myTextField -- call the variable the way you like
then, in your code, replace everywhere ' field "Field" ' with myTextField

Hope it will work...

Does the speed better of still too slow?

Kind 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 get lines from braces.

Post by samjith » Thu Oct 15, 2015 4:57 am

Hi Thierry,

I changed the code as you said.
I found that, the speed decrease with the text (lines in myTextField) increases.

Sometimes it will hang. if i increase the lines of text in field.

Is i have to do anything to speedup the process.

Please find the attached zip.
Please check with some text (100 lines) and large number of text (10000) in the field to see the difference

Thanks,
Samjith
Attachments
LC.zip
(1.89 KiB) Downloaded 236 times

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: How to get lines from braces.

Post by jameshale » Thu Oct 15, 2015 7:25 am

I thought your example as just a walk and thought it could be handled as such..

trying it on your sample (as sent to Thierry) it took 0.14 secs. (LC 7.1, retina iMac, el capitan)

Although looking at your sample I don't see any embedded tagging so my function is very much an overkill.

note: I have done nothing about the return characters as it seems they are required.

Code: Select all

function nottex textbuffer
   --   walk along string
   --   if char is "\" then we start a tag block
   --   add a tag to output stream and tack a tag on end
   --   if char is "{" we start a text block
   --   add char to output stream
   --   if char is "}" we are at end of text block
   --   add closing version of last tag
   put number of chars of textbuffer into stringln
   put 0 into depth
   put empty into newstring
   repeat with x = 1 to stringln
      put first char of textbuffer into achar
      delete first char of textbuffer --don't need it anymore
      switch
         case achar = "\"
            --build tag
            add 1 to depth
            put "<" after newstring
            put "</" into endtag[depth]
            put true into tag
            break
         case achar = "{"
            --close current tag and begin string, save tag's close and depth
            put ">" after newstring
            put ">" after endtag[depth]
            put false into tag
            break
         case achar = "}"
            --need to add this tag's close for this level
            put endtag[depth] after newstring
            subtract 1 from depth
            break
         default
            put achar after newstring --build new string
            if tag then put achar after endtag[depth] --construct closing tag for current level
      end switch
   end repeat
   return newstring
end nottex
James

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

Re: How to get lines from braces.

Post by samjith » Thu Oct 15, 2015 9:35 am

Hi James,

Woh! You are Great :) .

Very speedy. :shock:



Thanks
Samjith

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: How to get lines from braces.

Post by jameshale » Thu Oct 15, 2015 11:36 am

Glad it could be of use.

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

Re: How to get lines from braces.

Post by Thierry » Thu Oct 15, 2015 1:17 pm

Hi,

Happy for you that James came here with a nice and fast script.

As you started coding with regular expression, here is one solution
with regex which should be fast enough.

Code: Select all

on withRegex
   local newText
   put the text of field "fSource" into TexSource
   
   repeat for each line aLine in TexSource
      if matchText( aLine, "\\([^{]+){([^}]+)}", theTag, theValue) then
         put format( "<%s>%s</%s>\n", theTag, theValue, theTag) after newText
      else
         -- Take care here if no match or empty lines
         put aLine &cr after newText
      end if
   end repeat
   
   put newText into field "fResult"
end withRegex
Have fun,

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 get lines from braces.

Post by samjith » Thu Oct 15, 2015 2:16 pm

Ya, Brilliant work by James!

Seems some bug in Thierry's code


Thank you once again
Samjith

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

Re: How to get lines from braces.

Post by Thierry » Thu Oct 15, 2015 2:19 pm

samjith wrote: Seems some bug in Thierry's code

Samjith
Mmm, would be nice to tell a bit more, no?

So, what's wrong?

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

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: How to get lines from braces.

Post by jameshale » Thu Oct 15, 2015 2:39 pm

Thierry's code works fine.
very fine.
0.042 secs on my Mac for the sample stack text supplied.

As I said, my code was for the more complex example given in the OP where the string had nested tags.
I thought it may be possible to do this with REGEX as I have seen examples of nested parsing used in a couple of language parsing modules for C I think.
However my REGEX is not good enough to follow them and I am not even sure if LC's REGEX could manage.
The REGEX solution provided here however does not handle the original nested example.
But for the simpler data set which appears to be what you are working with, I'd go with Thierry's code.

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

Re: How to get lines from braces.

Post by Thierry » Thu Oct 15, 2015 2:47 pm

jameshale wrote:Thierry's code works fine.
very fine.
0.042 secs on my Mac for the sample stack text supplied.

As I said, my code was for the more complex example given in the OP where the string had nested tags.
I thought it may be possible to do this with REGEX as I have seen examples of nested parsing used in a couple of language parsing modules for C I think.
However my REGEX is not good enough to follow them and I am not even sure if LC's REGEX could manage.
The REGEX solution provided here however does not handle the original nested example.
But for the simpler data set which appears to be what you are working with, I'd go with Thierry's code.
Thanks James.

Did a quick comparison with the results of your solution and mine,
and got the same results except for extra spaces at the end of some lines which are left aside in my case.
Don't know what to do with these extra spaces, but it's easy to put them back in the resultsText if needed.

I write this code in a couple of minutes, based on the sample datas of the stack.
I was not aware of the nested example (not in the stack).
It can be done but need more free time...

Kind 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 get lines from braces.

Post by samjith » Fri Oct 16, 2015 5:28 am

Hi,

I apologize for my words :( .

I think there is some misunderstanding between us.

Actually my requirement is like this.
For example:-

\good{This
is \bad{
an} example
to show
you}

i need :-
<good>This is <bad>an</bad> example to show you</good>
But i didn't wite a single example like this in the field, i think that may confused Thierry
Am i understand right?


Thanks
Samjith

Post Reply