Find variable contained within another variable

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
mark_miller66
Newbie
Posts: 4
Joined: Sat May 30, 2015 4:44 pm
Location: PORT ORANGE, FL USA

Find variable contained within another variable

Post by mark_miller66 » Mon Aug 31, 2015 5:11 pm

Macroscheduler guide shows me the following:
Simple expressions can contain only two parts separated by one of the following operators:
= Equals
> Greater than
< Less than
<> Not Equal

Is there an operator to determine if one variable is contained within another variable?
For example, let x=abcdefg let y= dg
I require the result to show that both the letters 'd' and 'g' are contained in the variable x

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Find variable contained within another variable

Post by hagchr » Mon Aug 31, 2015 6:13 pm

Hi, I am not aware of any such operator, however you can easily set up a subroutine that does the same:

Code: Select all

Let>x=abcdefg
Let>y=dg

Gosub>match,y,x,result
mdl>result

SRT>match
    //SRT to match y in x and return true/false in third input variable
    Let>LOCALVARS=1
    Let>y=match_VAR_1
    Let>x=match_VAR_2
    Let>var_out=match_VAR_3
    RegEx>.,y,0,m1,nm1,0
    If>nm1>0
        Let>flag=true
    Else>
        Let>flag=false
    Endif
    Let>ct=0
    While>ct<nm1
        Add>ct,1
        RegEx>m1_%ct%,x,0,m2,nm2,0
        If>nm2=0
            Let>flag=false
        Endif
    EndWhile
    Let>LOCALVARS=0
    Let>%var_out%=flag
END>match

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Find variable contained within another variable

Post by hagchr » Mon Aug 31, 2015 6:38 pm

Hi, previous version matched irrespective of case. I added a fourth parameter 1/0 where you can choose if case sensitive or not (ie should y=dG match or not. If case sensitive, then no, otherwise yes)

Code: Select all

Let>x=abcdefg
Let>y=dg

Gosub>match,y,x,result,0

mdl>result

SRT>match
    //SRT to match y in x and return true/false in third input variable
    //Fourth input variable indicates if case sensitive or not 1/0
    Let>LOCALVARS=1
    Let>y=match_VAR_1
    Let>x=match_VAR_2
    Let>var_out=match_VAR_3
    Let>case_ind=match_VAR_4
    RegEx>.,y,0,m1,nm1,0
    If>nm1>0
        Let>flag=true
    Else>
        Let>flag=false
    Endif
    Let>ct=0
    While>ct<nm1
        Add>ct,1
        If>case_ind=1
            Let>tmp=m1_%ct%
            Let>tmp0=(?-i)%tmp%
        Else
            Let>tmp0=m1_%ct%
        Endif
        RegEx>tmp0,x,0,m2,nm2,0
        
        If>nm2=0
            Let>flag=false
        Endif
    EndWhile
    Let>LOCALVARS=0
    Let>%var_out%=flag
END>match

mark_miller66
Newbie
Posts: 4
Joined: Sat May 30, 2015 4:44 pm
Location: PORT ORANGE, FL USA

Re: Find variable contained within another variable

Post by mark_miller66 » Mon Aug 31, 2015 8:06 pm

Thank you for pointing me to Regular Expressions in your code.
Masterful answer!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts