Page 3 of 3

Re: Regex for LiveCode

Posted: Mon Sep 02, 2024 9:42 am
by kaveh1000
You're my hero Stam! OK, Seems I need to start using arrays more. And I don't think I ever used styledtext. Will go thro it all and come back with any questions.

But let us keep the pressure for regex, as there are very complex transformations that can only be done with regex. e.g. I have several hundred patterns similar to:

Code: Select all

(?<=\n)(?sx)               #Note the ?x allows me to put comments in regex
\[(.+?)\]                   
\s(.+?),\s+                 
([^,]+?),\s+                
([^,]+?)\s+                 
(\d+)                       
(?:\((\d+?)\))?             
\s+\((\d{4})\)              
 (?:\s+([^\s]+?) )?         
 (?:\s?[-–?]+\s?([^\s]+?))? 
\.(?=\n)
Very happy to be in touch and thanks again. I will be sending my regex backreferencing macros.

Regards
Kaveh

Re: Regex for LiveCode

Posted: Mon Sep 02, 2024 11:46 am
by kaveh1000
Oh, and searching for StyledText I came across my own posts in this thread:
viewtopic.php?t=33764
:-)

Re: Regex for LiveCode

Posted: Mon Sep 02, 2024 5:22 pm
by stam
I think you may be crediting me with Bernd's input ;)

Re: Regex for LiveCode

Posted: Mon Sep 02, 2024 5:47 pm
by kaveh1000
No I realise it was Bernd. But clearly I had heard of styledText but never used it.

FYI you have inspired me to look again. I still have a mental block with multidimentional arrays, but I have written a loop now and the text is coloured in .5 seconds, so not too bad! Working on it...

Re: Regex for LiveCode

Posted: Tue Sep 03, 2024 4:24 pm
by bn
Hi all,

I have modified my version of coloring text in the "OriginalText.txt" file Kaveh provided.

This is just to make it functional. No need to use it... Just a demo how to use styledText to do the job.

In the first version I did not take into account that the .txt file is UTF-8 encoded.
Furthermore I removed all html code and line formatting in the description texts (that may not be what is wanted but it made it easier to code and Kaveh did not indicate a use case for colorisation.)

Kind regards
Bernd

Re: Regex for LiveCode

Posted: Tue Sep 03, 2024 4:40 pm
by kaveh1000
Oh my goodness. Stam was right that I gave him credit for the app you kindly produced! I did not even look at the author. I assumed it was him. You are both my heroes now!!

I will give this a go later. Determined to understand styledText. :-)

Re: Regex for LiveCode

Posted: Tue Sep 03, 2024 6:42 pm
by stam
bn wrote: Tue Sep 03, 2024 4:24 pm Just a demo how to use styledText to do the job.
A masterclass as always Bernd ;)

Re: Regex for LiveCode

Posted: Tue Sep 03, 2024 7:43 pm
by bn
Hi Kaveh and Stam,

Thank you for your kind words.

I think the easiest way to understand styledText is to make a small text in a field with, say, 4 lines of 3 words each. Then set the color of 1 word of a line to any color you want.
Now in a button you

Code: Select all

put the styledText of field "myField" into tArray
breakpoint
Inspect the array and see how styledText handles the, well, styles.
Kind regards
Bernd

Re: Regex for LiveCode

Posted: Wed Sep 04, 2024 9:07 am
by kaveh1000
Thank you Bernd. Extremely informative. For a start, I had no idea about breakpoint!!

I have tried it and can see how it works. Perhaps this will help me with my irrational fear of arrays!

And a quick question - am I right that arrays are much faster than loops, say, in general?

Re: Regex for LiveCode

Posted: Wed Sep 04, 2024 9:12 am
by stam
I think manipulating arrays is faster than manipulating strings.
No way to avoid looping!

Re: Regex for LiveCode

Posted: Wed Sep 04, 2024 3:47 pm
by bn
kaveh1000 wrote: Wed Sep 04, 2024 9:07 am And a quick question - am I right that arrays are much faster than loops, say, in general?
As Stam pointed out that you will have to loop.
But there are different loops, some faster some slower.

In my experience it all depends on what you are trying to achieve.
If you have a list, like in your example xml, working with the list in a repeat loop:

Code: Select all

repeat for each line aLine in myList
In this form aLine contains the text of that line and you can act upon it
The same list in the form

Code: Select all

repeat with i = 1 to the number of lines of myList
   put line i of myList into tLineText
   ... 
is very slow for longer lists because on each iteration you force LC to fetch line i of myList to put it into a variable.

On the other hand creating an array from a list

Code: Select all

split myList by return
has its overhead too. Creating an array is not free.

Then it also depends on how you access the array.
In above sample you have an array that is numerically indexed by a key
However in arrays the keys are not sorted numerically. You would have to put the keys into a list and sort that list to use the keys from the list to access the array values.
Stam uses a faster approach:

Code: Select all

repeat for each element anElement in tArray
But you have to know whether your particular array is suited for this approach.


I attach a stack that demonstrates different approaches to loop for lists. Please have a look at the code.
Click on a radio button and then try different versions of loops.
It uses the colorNames of LC to create lists of different lengths to compare speed.

Kind regards
Bernd