Easier way to do this? Find variables in text

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: Klaus, FourthWorld, heatherlaine, kevinmiller

japino
Posts: 62
Joined: Sun Oct 14, 2012 10:56 am

Easier way to do this? Find variables in text

Post by japino » Sun May 05, 2019 12:13 pm

I’m going to work on a small project which includes finding all variables in a long text. I’ve done something like this before and I was wondering if what I did in the past can be coded better.
The variables take this form: @{xxx}

What I did before is this:
- search from the beginning of text and find @{
- search again using an offset and find }
- put character x though y in a Livecode variable
- use a repeat loop to do subsequent searches with offsets

The end result is Livecode variable with a list of variables found in the text, delimited by Return characters

Just wondering if there would be an easier way to find all the variables than what I described above

bogs
Posts: 4007
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easier way to do this? Find variables in text

Post by bogs » Sun May 05, 2019 12:50 pm

What you have certainly looks viable to me, but I am curious as to how the text is setup. For instance, if the text is all separated by spaces but the variable is not (as you put it), you could use word to search, something like this, although I'm sure it could be shortened -

Code: Select all

on mouseUp
   repeat for each word xVar in field 1 of this stack
      if (character 1 of xVar is "@") and (character -1 of xVar is "}") then
         put xVar & cr after tmpMatch
         -- do whatever it is you want to do with it 
      end if
   end repeat
   answer tmpMatch
end mouseUp
// The if/then here could have been put on one line as well, I separated it out for clarity only,
//  but if you had more than one thing happening in it, you might as well leave it.
Selection_001.png
It is a match baby!!
*Edit - I left out things that would make this a lot faster, for instance, putting the field into a variable then searching the variable, locking messages, etc, because I am guessing you already know these tricks.
Image

Klaus
Posts: 11335
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Easier way to do this? Find variables in text

Post by Klaus » Sun May 05, 2019 1:21 pm

Or like this:

Code: Select all

...
put fld 1 into tText
replace SPACE with CR in tText
filter tText with "@{*"

## Just to be sure:
filter tText with "*}"
put tText
...
Many, many ways to skin a cat in LC. :D

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2192
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Easier way to do this? Find variables in text

Post by [-hh] » Sun May 05, 2019 1:51 pm

Yet another option.

Code: Select all

-- str is your long text
function getVariables str
  set linedel to "@{"
  set itemdel to "}"
  delete line 1 of str
  repeat for each line L in str
    put item 1 of L & cr after sout
  end repeat
  return sout
end getVariables
Do you intend to fill in the variables into your long text later on? Then read about function merge in the Dictionary.
shiftLock happens

bogs
Posts: 4007
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easier way to do this? Find variables in text

Post by bogs » Sun May 05, 2019 1:53 pm

Yup, many ways indeed. I wonder if anyone will pop in with the ultimate regex solution, cause that is the only thing I think we haven't covered so far in normal use.

Not saying I would understand it mind you, just wondering about it :wink:
Image

japino
Posts: 62
Joined: Sun Oct 14, 2012 10:56 am

Re: Easier way to do this? Find variables in text

Post by japino » Sun May 05, 2019 4:34 pm

Thanks a lot everybody! This is most helpful!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 664
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Easier way to do this? Find variables in text

Post by Thierry » Sun May 05, 2019 5:26 pm

bogs wrote:
Sun May 05, 2019 1:53 pm
I wonder if anyone will pop in with the ultimate regex solution, cause that is the only thing I think we haven't covered so far in normal use.
Hi bogs,

Here is one:

Code: Select all

if sunnYextract( theText, "(?m)@\{(\w+)}", "\1\n", R) then put R
 
and, of course if you have such a text as an input (theText):

"xxx @{abc} yyy @{d1} zzz @{Z} qqqq"

the result (R) will be:

abc
d1
Z

Regards,
Thierry
Last edited by Thierry on Mon May 06, 2019 8:52 am, edited 1 time in total.
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

Klaus
Posts: 11335
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Easier way to do this? Find variables in text

Post by Klaus » Sun May 05, 2019 5:37 pm

Thierry wrote:
Sun May 05, 2019 5:26 pm
Did you call me?
Yes, we all habe been standing in front of a mirror shouting:
Beetl... erm. Captain REGEX!
Captain REGEX!
Captain REGEX!

:D :D :D

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 664
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: Easier way to do this? Find variables in text

Post by Thierry » Sun May 05, 2019 5:40 pm

Klaus wrote: ...
Sorry, didn't find it funny at all :roll:

Best
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

bogs
Posts: 4007
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easier way to do this? Find variables in text

Post by bogs » Sun May 05, 2019 5:47 pm

Thierry wrote:
Sun May 05, 2019 5:26 pm
bogs wrote:
Sun May 05, 2019 1:53 pm
I wonder if anyone will pop in with the ultimate regex solution, cause that is the only thing I think we haven't covered so far in normal use.
... Not saying I would understand it mind you, just wondering about it :wink:
Hi Mister bogs,
Did you call me?
Actually, you were precisely who I had in mind, I always look forward to seeing what you come up with :D

Right now, studying the speed the various options presented, no surprise, mine is by far the slowest heh.

Hermann's actually didn't work until I changed the lineDelimiter. Maybe can't have 2 characters as a delimiter?

Code: Select all

set linedel to "@" --<-- couldn't run till I changed it to this...
After the change, it is the fastest, but produces some different looking results.
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2192
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Easier way to do this? Find variables in text

Post by [-hh] » Sun May 05, 2019 5:56 pm

bogs wrote:Hermann's actually didn't work until I changed the lineDelimiter. Maybe can't have 2 characters as a delimiter?
Just try with a LC version > 6,
linedelimiter, itemdelimiter etc. can be multiple chars: Starting with LC 7.
And my solution doesn't implicitly assume that "@{...}" is surrounded by spaces or returns (as some others do).
shiftLock happens

Klaus
Posts: 11335
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Easier way to do this? Find variables in text

Post by Klaus » Sun May 05, 2019 6:16 pm

Thierry wrote:
Sun May 05, 2019 5:40 pm
Klaus wrote: ...
Sorry, didn't find it funny at all :roll:

Best
Sorry for this, really did not mean to insult you! :oops:

bogs
Posts: 4007
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easier way to do this? Find variables in text

Post by bogs » Sun May 05, 2019 8:32 pm

[-hh] wrote:
Sun May 05, 2019 5:56 pm
bogs wrote:Hermann's actually didn't work until I changed the lineDelimiter. Maybe can't have 2 characters as a delimiter?
Just try with a LC version > 6,
linedelimiter, itemdelimiter etc. can be multiple chars: Starting with LC 7.
And my solution doesn't implicitly assume that "@{...}" is surrounded by spaces or returns (as some others do).
Ah, that would explain it! I'll move into a higher edition to do the testing, as it is (almost certain) that the OP is not using anything as old and decrepit as I do :D

My apologies, Hermann, I should have known better :oops:
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6266
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Easier way to do this? Find variables in text

Post by dunbarx » Sun May 05, 2019 9:23 pm

Bogs.

I stuck with v6 for way too long, until last year. Worried about stability, and I never use unicode. And this even though I thought the single enhancement of the itemDelimiter to strings of any length was VERY useful indeed, and warranted the upgrade all on its own.

Jacque gently admonished me to get with it already.

So.

Get with it already, at least to 8.1.10. I have had issues with several of the v9's.

Craig

bogs
Posts: 4007
Joined: Sat Feb 25, 2017 10:45 pm

Re: Easier way to do this? Find variables in text

Post by bogs » Sun May 05, 2019 9:34 pm

Well, I still haven't really gone up to v6.x yet, heh, and none of the versions past 7 really run all that well on my distros of 'nix (which are not 'buntu), but aside from that, I still program for a number of bsd boxes I have. I'll likely be in Mc for the rest of my run, using a 2.7 or lower format :shock:

When I am in an Lc IDE, 6.x is usually the highest I go, more often than not I am poking around RR 2.x. The only times I'm any higher than that really is to poke around little things like this, which I find interesting enough to warrant the investigation :oops:
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”