Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Programming
 PowerHome Messageboard : PowerHome Programming
Subject Topic: Set variable value in case statement Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
patrickm
Senior Member
Senior Member


Joined: February 22 2007
Location: United States
Online Status: Offline
Posts: 188
Posted: July 25 2013 at 10:31 | IP Logged Quote patrickm

I am trying to write some macros that use case statements
to read and write some variables. If I hard code the
variable name it works fine.

ph_setglobal_s("SB_STATE_1", case("{SB_MODE_1}" when
"play" then"2" when "pause" then "3" when "stop" then
"0"))

What I would like to do is access the variables in a
loop counter.

ph_setglobal_s("SB_STATE_" + "[LOCAL9]", case("SB_MODE_"
+ "[LOCAL9]"
when "play" then"2" when "pause" then "3" when "stop"
then "0"))

That returns a zero but the variable doesn't change.

What is the correct way of doing this?

Thanks
Patrick


Edited by patrickm - July 25 2013 at 10:34
Back to Top View patrickm's Profile Search for other posts by patrickm
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: July 25 2013 at 11:49 | IP Logged Quote dhoward

Patrick,

You can simplify the code a little bit by using this:

ph_setglobal_s("SB_STATE_[LOCAL9]", case("SB_MODE_[LOCAL9]"
when "play" then"2" when "pause" then "3" when "stop"
then "0"))

If [LOCAL9] contains a value of 3, then after the substitution, our statement to be evaluated will look like this:

ph_setglobal_s("SB_STATE_3", case("SB_MODE_3"
when "play" then"2" when "pause" then "3" when "stop"
then "0"))

Herein lies actual problem. The case statement is now switching on the string value "SB_MODE_3" and not what is actually contained in the global variable SB_MODE_3. You could try to use variable substitution to handle this (like in your first statement) by using something like this:

case("{SB_MODE_[LOCAL9]}" when...

but it wont work and that kind of substitution gets ugly.

To fix it, use this:

ph_setglobal_s("SB_STATE_[LOCAL9]", case(ph_getglobal_s("SB_MODE_[LOCAL9]")
when "play" then"2" when "pause" then "3" when "stop"
then "0"))

I tested this and it works.

Hope this helps,

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
patrickm
Senior Member
Senior Member


Joined: February 22 2007
Location: United States
Online Status: Offline
Posts: 188
Posted: July 25 2013 at 13:27 | IP Logged Quote patrickm

Dave,
Thanks for the explanation.

Patrick
Back to Top View patrickm's Profile Search for other posts by patrickm
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum