Easier way to do this? Find variables in text
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Easier way to do this? Find variables in text
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
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
Re: Easier way to do this? Find variables in text
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 -
*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.
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.
![Image](https://s7.postimg.cc/v87si6xuj/Forum_Signature.png)
Re: Easier way to do this? Find variables in text
Or like this:
Many, many ways to skin a cat in LC. ![Very Happy :D](./images/smilies/icon_biggrin.gif)
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
...
![Very Happy :D](./images/smilies/icon_biggrin.gif)
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Easier way to do this? Find variables in text
Yet another option.
Do you intend to fill in the variables into your long text later on? Then read about function merge in the Dictionary.
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
shiftLock happens
Re: Easier way to do this? Find variables in text
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 :wink:](./images/smilies/icon_wink.gif)
Not saying I would understand it mind you, just wondering about it
![Wink :wink:](./images/smilies/icon_wink.gif)
![Image](https://s7.postimg.cc/v87si6xuj/Forum_Signature.png)
Re: Easier way to do this? Find variables in text
Thanks a lot everybody! This is most helpful!
Re: Easier way to do this? Find variables in text
Hi bogs,
Here is one:
Code: Select all
if sunnYextract( theText, "(?m)@\{(\w+)}", "\1\n", R) then put R
"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.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Easier way to do this? Find variables in text
Sorry, didn't find it funny at allKlaus wrote: ...
![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
Best
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: Easier way to do this? Find variables in text
Actually, you were precisely who I had in mind, I always look forward to seeing what you come up with
![Very Happy :D](./images/smilies/icon_biggrin.gif)
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...
![Image](https://s7.postimg.cc/v87si6xuj/Forum_Signature.png)
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Easier way to do this? Find variables in text
Just try with a LC version > 6,bogs wrote:Hermann's actually didn't work until I changed the lineDelimiter. Maybe can't have 2 characters as a delimiter?
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
Re: Easier way to do this? Find variables in text
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[-hh] wrote: ↑Sun May 05, 2019 5:56 pmJust try with a LC version > 6,bogs wrote:Hermann's actually didn't work until I changed the lineDelimiter. Maybe can't have 2 characters as a delimiter?
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).
![Very Happy :D](./images/smilies/icon_biggrin.gif)
My apologies, Hermann, I should have known better
![Embarassed :oops:](./images/smilies/icon_redface.gif)
![Image](https://s7.postimg.cc/v87si6xuj/Forum_Signature.png)
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Easier way to do this? Find variables in text
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
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
Re: Easier way to do this? Find variables in text
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
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![Embarassed :oops:](./images/smilies/icon_redface.gif)
![Shocked :shock:](./images/smilies/icon_eek.gif)
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
![Embarassed :oops:](./images/smilies/icon_redface.gif)
![Image](https://s7.postimg.cc/v87si6xuj/Forum_Signature.png)