cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Help with If Statement

I'm currently trying to write a script that reads a column from my data table, and runs a different set of functions depending on the value of that column. For example,

 

If the data in Column A = A, create a new column called "A" and run the functions (A to C) below.

If the data in Column A = B, create a new column called "B" and run the functions (D to F) below instead.

Else, create a new column called "N/A".

 

When I run my script, it will always create the new column "N/A" even if the value is "A" or "B". It will also not run any of the functions I have written for the "A" and "B" conditions. I feel like I'm missing something very obvious.

 

 

dt = Current Data Table();
If(
:Column A == "A", dt << New Column( "A", character, nominal ),
Function A
Function B
Function C

:Column B == "B", dt << New Column( "B", character, nominal ),
Function D
Function E
Function F

dt << New Column( "N/A", character, nominal )
)

 

3 REPLIES 3
Craige_Hales
Super User

Re: Help with If Statement

If Secretsmight help. Commas separate the clauses in an if statement. Semicolons separate statements within a clause.

You probably want something like this

 

dt = Current Data Table();
If( :Column A == "A", // condition, followed by a comma
     dt << New Column( "A", character, nominal ); // one or more stmts
     Function A(); // with ; separators
     Function B();
     Function C(); // last statement has optional ; and required comma next line
, /* else if */ :Column B == "B", // 2nd condition followed by comma
     dt << New Column( "B", character, nominal );
     Function D();
     Function E();
     Function F() // again, another comma. ; omitted for example...I prefer to use them
, /* else */ 
     dt << New Column( "N/A", character, nominal ) // no more commas, this is the otherwise
);

I like to indent and comment the /* else if */ and /* else */ to make it easier to follow.

 

 

Craige
txnelson
Super User

Re: Help with If Statement

Given the scenarios you specified, the script below, generates the 3 cases

     A exists in Column A

     B exists in Column B

     Neither A or B exist

ab.PNG

Names Default To Here( 1 );

A = Function( {},
	Show( "Function A" )
);
B = Function( {},
	Show( "Function B" )
);
C = Function( {},
	Show( "Function C" )
);
D = Function( {},
	Show( "Function D" )
);
EE = Function( {},
	Show( "Function EE" )
);
F = Function( {},
	Show( "Function F" )
);

// First Example Column A has the value A
dt1 = New Table( "Example1",
	Add Rows( 1 ),
	New Column( "Column A", Character, "Nominal", Set Values( {"A"} ) ),
	New Column( "Column B", Character, "Nominal", Set Values( {""} ) )
);
Wait( 5 );

If(
	:Column A[1] == "A",
		dt1 << New Column( "A", character, nominal );
		A();
		B();
		C();,
	:Column B[1] == "B", dt1 << New Column( "B", character, nominal );
		D();
		EE();
		F();
	, 
	dt1 << New Column( "N/A", character, nominal )
);


// Second Example Column B has the value B
dt2 = New Table( "Example2",
	Add Rows( 1 ),
	New Column( "Column A", Character, "Nominal", Set Values( {""} ) ),
	New Column( "Column B", Character, "Nominal", Set Values( {"B"} ) )
);
Wait( 5 );

If(
	:Column A[1] == "A",
		dt2 << New Column( "A", character, nominal );
		A();
		B();
		C();,
	:Column B[1] == "B", dt2 << New Column( "B", character, nominal );
		D();
		EE();
		F();
	, 
	dt2 << New Column( "N/A", character, nominal )
);


// Third Example Neither column A has and A nor Column B has a B
dt3 = New Table( "Example3",
	Add Rows( 1 ),
	New Column( "Column A", Character, "Nominal", Set Values( {""} ) ),
	New Column( "Column B", Character, "Nominal", Set Values( {""} ) )
);
Wait( 5 );

If(
	dt3:Column A[1] == "A",
		dt3 << New Column( "A", character, nominal );
		A();
		B();
		C();,
	dt3:Column B[1] == "B", dt3 << New Column( "B", character, nominal );
		D();
		EE();
		F();
	, 
	dt3 << New Column( "N/A", character, nominal )
);

It is my guess, that this is not what you want

Jim
Craige_Hales
Super User

Re: Help with If Statement

Good point about the column name changing. It makes it harder to write the JSL for subsequent processing. Probably you want the same column name for all the cases, something like "status" or "action type".

 

Craige