Determining the variable name

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
arobin4
Junior Coder
Posts: 33
Joined: Sat Apr 07, 2012 5:41 am

Determining the variable name

Post by arobin4 » Thu Aug 07, 2014 9:15 pm

Hello Forum,

I am trying to determine the name of a variable based on the value of the variable, For example in:

Let>green_alarm=ssh
Let>red_alarm=http

I need a code construct to determine which variable has the value "http" without having to perform an explicit 'if' check on each variable. For simplicity the variable values in my example are static, but in my actual use-case the value of the variables will be calculated (dynamic). Is there a "find" type of command to accomplish this?

Thank you

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Determining the variable name

Post by Marcus Tettmar » Fri Aug 08, 2014 6:59 am

It sounds like what you really want is an array, but instead of an array indexed by numbers, indexed by a string which in this case is a colour. If we call it alarm_ and we have array entries alarm_green and alarm_red we could have an array of the index names (green, red, ...) then we could use some loops to do your "FIND". You'd need an array for the index names, and a repeat/until loop for the main array, which stops when found.


Sent from my iPad using Tapatalk
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

arobin4
Junior Coder
Posts: 33
Joined: Sat Apr 07, 2012 5:41 am

Re: Determining the variable name

Post by arobin4 » Fri Aug 08, 2014 6:20 pm

Marcus,
Thank you!

Aaron

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

Re: Determining the variable name

Post by hagchr » Fri Aug 08, 2014 6:36 pm

Not sure if this would work for you but one way could be to pull the variable names and values into a string and then use RegEx to search for the variable name.

//Assign variables
Let>alarm_green=http
Let>alarm_orange=xyz
Let>alarm_red=ssh

//Create reference string to search
Let>refstring=alarm_green=%alarm_green%%CRLF%alarm_orange=%alarm_orange%%CRLF%alarm_red=%alarm_red%

//Value you are searching for
Let>searchstring=ssh

//Do the search
Let>tmp0=(?m)[^=\n]+(?==%searchstring%)
RegEx>tmp0,refstring,0,M,NM,0

If>NM>0
MessageModal>M_1
ELSE
MessageModal>Value not assigned to variable
EndIf

  View Snippet Page

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