I would like to transform my if() conditional to switch() from this:
if($configuration['application'][$applicationName]['subdomain'] == true){
foreach($configuration['language'] as $language){
if($language['abbreviation'].'.'.$configuration['application'][$applicationName]['domain'] == $_SERVER['HTTP_HOST']){
$_SESSION['language'] = $language['abbreviation'];
}
}
// If no subdomain detected and redirection is enabled, set default language
if(!isset($_SESSION['language'])){
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
}
}
else {
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
}
To this:
switch($configuration['application'][$applicationName]['subdomain']){
case true:
foreach($configuration['language'] as $language){
if($language['abbreviation'].'.'.$configuration['application'][$applicationName]['domain'] == $_SERVER['HTTP_HOST']){
$_SESSION['language'] = $language['abbreviation'];
break;
}
}
default:
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
break;
}
I think it should be the same but it behaves differently ...
Switch is not working properly ...
I have reformatted your code, please check to make sure it is still correct.
As for your problem, to begin with you are missing a break; statement at the end of your case true: statement. (The break inside the foreach loop simply breaks out of that loop, not the case itself).
Related
Below is the code in javascript. I need to convert it into Python. Having a multiple switch cases with one return value.:
textOfBetType: function(tip) {
switch (tip) {
case "tip1":
case "tip0":
case "tip2":
return "Livewette 0:0"
//return "Sieger (3-Weg)"
case "tipOver":
case "tipUnder":
return "Über/Unter 2,5";
}
return null;
}
I used "if and else" statement in Python.
def text_of_bet_type(self, tip):
if tip == "tip1" or tip == "tip0" or tip == "tip2":
return "Livewette 0:0"
else:
return 'null';
but is there any other way to do this this..
I receive via POST a value. Then, I´m comparing the value received (1, 2, 3, 4, 5) with variables pre defined in my code.
Is it possible to do it with FOR or another way to simplify it without changing the functionality of the code?
Yes, I need to receive the value as number and compare it with variables (no MYSQL).
I set on each test the name, eg: $varname = "Paul";
Here´s what I´m doing and what I´d like to change.
Thanks a lot
// from previous page with input name thenumber
$thenumber = $_POST['thenumber'];
$option1 = "1";
$option1 = "2";
$option1 = "3";
$option1 = "4";
$option1 = "5";
$option1 = "6";
...
...
More options
if($thename == $option1)
{
$varname = "Paul";
}
else if ($thename == $option2)
{
$varname = "Louie";
}
else if ($thename == $option3)
{
$varname = "Dimitri";
}
...
...
...
It would be much cleaner to do this with a switch.
I don't think using a for loop will be a good idea.
Be sure to put a break after each case ends.
The default case is when $thename is none of the numbers in the cases.
switch ($thename) {
case 1:
$varname = "paul";
break;
case 2:
$varname = "Louie";
break;
case 3:
$varname = "Dimitri";
break;
...
default:
$varname = "default_name";
break;
}
I try to run a code, but I cannot understand what throw statement do in this part, I thought that we can use ´throw´ statement in try-catch block.
any can help me with this example:
switch(npt) {
case 1: {
a = NPoint1;
b = NLine1;
break;
}
.
.
.
case 5: {
a = NPoint2;
b = NLine2;
break;
}
default:
printf("what you entered is wrong");
throw 1;
};
return 1;
}
thanks in advance for any help
throw is catched by the nearest try-catch block. If it's not in your own code, it's in the code that invoked your's and so on up the stack.
I'm working on an experience design project for one of my classes using a rotary phone and arduino kit to create a game based on automated phone menus. Serial input from the rotary dial is running through arduino and now I am using processing to write the menu.
I have an outline of actions and have started to code some if then statements to get going but now I have stumbled upon case and switch.
I am completely new to this but have learned a lot in class.
My question is how do I make a continuous set of nested if/then statments OR use case and switch to move through a series of prompts and inputs?
Here is my sketch so far:
import processing.serial.*;
Serial port; // Create object from Serial class
float val; // Data received from the serial port
boolean task1prompted;
boolean task1;
boolean task2;
boolean dialed;
PFont font;
void setup() {
size(800, 400);
background(0, 0, 0);
smooth();
// IMPORTANT NOTE:
// The first serial port retrieved by Serial.list()
// should be your Arduino. If not, uncomment the next
// line by deleting the // before it. Run the sketch
// again to see a list of serial ports. Then, change
// the 0 in between [ and ] to the number of the port
// that your Arduino is connected to.
//println(Serial.list());
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 9600);
task1 = false;
task2 = false;
task1prompted = false;
font = createFont("Arial", 32);
textFont(font, 32);
textAlign(CENTER);
}
void draw() {
if (port.available() > 0) { // If data is available,
val = port.read(); // read it and store it in val
if (val >= 48 && val <= 57) {
val = map(val, 48, 57, 0, 9); // Convert the value
}
println(val);
}
if (val == 97) {
println("dialing");
}
if (val == 98){
println("dialed");
dialed = true;
}
/// switch will activate the task1 variable.
// Play sound file for the prompt.
if (task1prompted == false){
delay(1000);
println("for spanish, press one. for french, press 2...");
task1prompted = true;
}
task1 = true;
if (task1 == true && dialed == true) {
///play sound file
if (val == 5) {
println("Thank you for playing... Blah blah next prompt.");
dialed = true;
task1=false;
task2=true;
} else
if (val != 5) {
println("We're sorry, all of our international operators are busy");
task1 = true;
task2 = false;
dialed = false;
}
}
else
if (task2 == true){
delay(1000);
println("task2 start");
}
}
My instructor helped me to get this far and I have been scouring for answers on how to keep going on to the next task/prompt. Would it be easier to use case and switch? And am I even doing nested if statements the right way?
Well I just tried this out with sketch and case commands as follows:
/// Switch will activate the task1 variable.
// Play sound file for the prompt.
if (task1prompted == false){
delay(1000);
println("for spanish, press one. for french, press 2...");
task1prompted = true;
}
task1 = true;
if (task1 == true && dialed == true) {
///Play sound file
int lang = (int)(val+0);
switch(lang) {
case 1:
case 2:
case 3:
case 4:
println("sorry no international operators"); // If 1-4 go back to choices
task1 = true;
task2 = false;
dialed = false;
break;
case 5:
println("thank you, move to next prompt"); // If 5 go to next prompt
task1=false;
task2=true;
dialed = true;
break;
case 6:
case 7:
case 8:
case 9:
case 0:
println("not a valid option, you lose"); // If not 1-5 go back to beginning
task1=false;
task2=false;
dialed = true;
break;
}
if (task2prompted == false){
delay(1000);
println("please listen while we test the line");
task2prompted = true;
}
task2 = true;
if (task2 == true && dialed == true) {
} ///Play sound file
int tone = (int)(val+0);
switch(tone) {
case 1:
case 2:
case 3:
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
println("not a valid connection, you lose"); // If not 4 go back to beginning
task2 = false;
task3 = false;
dialed = false;
break;
case 4:
println("thank you, move to next prompt"); // If 4 move to next prompt
task2=false;
task3=true;
dialed = true;
break;
}
}
}
I'm still confused on how to make this have levels and not all happen simultaneously.
You might want to look up finite state machines. It's a pretty common approach to dealing with event driven user interfaces.
I'm not entirely sure what your question is but maybe something here will answer it, if not just clarify what you're looking for
With case statements you don't need to make a case for every single output that can happen. the way you can avoid doing that is by using a default statement
Example:
switch(tone) {
case 4:
println("thank you, move to next prompt"); // If 4 move to next prompt
task2=false;
task3=true;
dialed = true;
break;
default:
println("not a valid connection, you lose"); // If not 4 go back to beginning
task2 = false;
task3 = false;
dialed = false;
}
The default case doesn't need a break because it is at the end. But essentially it is the catch all case if nothing else is hit.
Also in some of your code above
if (val == 97) {
println("dialing");
}
if (val == 98){
println("dialed");
dialed = true;
}
it is better to use an "else if" to make it not have to check through both if one is correct
if (val == 97) {
println("dialing");
}
else if (val == 98){
println("dialed");
dialed = true;
}
I am finally getting around to writing stuff in cfscript, and so I am starting with writing some needed formatting functions. Here is an example:
Function FormatBoolean(MyBool, Format) {
Switch(Format){
Case "YES/NO":{
If (MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
What I would like to do is make Format an optional argument. If you don't include the argument, the function will currently still run, but it won't find format, and it seems that cfparam did not get translated to cfscript.
Will I just have to check if Format is defined and give it a value? Or is there a nicer way of doing this?
Thanks
Personally I prefer to set defaults to this kind of arguments. Also I've refactored function a bit... But not tested :)
function FormatBoolean(required any MyBool, string Format = "") {
switch(arguments.Format) {
case "YES/NO":
return YesNoFormat(arguments.MyBool EQ 1);
default:
return (arguments.MyBool eq 1) ? "Yes" : "";
}
}
Please note that (arguments.MyBool EQ 1) may be replaced with (arguments.MyBool), so it covers all boolean values. You may be interested to make it more reliable, something like this (isValid("boolean", arguments.MyBool) AND arguments.MyBool) -- this should allow to check any value at all.
All variables passed into a function are available to access programmatically via the ARGUMENTS scope. You can refer to it as if it were an array (because it is), as well as standard struct key access (which I've done for you below for the MyBool parameter):
<cfscript>
Function FormatBoolean(MyBool) {
var theFormat = '';
if (ArrayLen(ARGUMENTS) GT 1)
theFormat = ARGUMENTS[2];
Switch(theFormat){
Case "YES/NO":{
If (ARGUMENTS.MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (ARGUMENTS.MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
</cfscript>
Add your preferred additional levels of data validation as necessary.