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

Search specific name by IF

Hello

I have set up a New Column "Pin Name"

and would like to add a number of PIN

It means if you contain gp_0 in the column "Test Name" it should be GP_0

or if you have contains gp_1 in test name it should be GP_1 in Column "Pin Name"

or if you have contains gp_11 in test name it should be GP_11 in Column "Pin Name"

I wrote the script

Column(dt,"PIN_NAME") << Formula ( If ( Contains( :TEST_NAME, "gp_0" ), "GP_0",
If( Contains( :TEST_NAME, "gp_1" ), "GP_1",
If( Contains( :TEST_NAME, "gp_10" ), "GP_10",
If( Contains( :TEST_NAME, "gp_11" ), "GP_11",
If( Contains( :TEST_NAME, "gp_12" ), "GP_12" ))))));
 
but I see the formula add GP_1 even I have gp_11 in test name.
so my question is, how I can set exactly name for searching?

Dennisbur_0-1696416826951.png

 

2 REPLIES 2
mmarchandTSI
Level V

Re: Search specific name by IF

JMP will end the If() loop as soon as a condition is met.  Technically, "gp_11" does contain "gp_1"....

You could rewrite the If() loop backwards, like

If(
	Contains( :TEST_NAME, "gp_12" ), "GP_12",
	Contains( :TEST_NAME, "gp_11" ), "GP_11",
	Contains( :TEST_NAME, "gp_10" ), "GP_10",
	Contains( :TEST_NAME, "gp_1" ), "GP_1"
);

But that's going to get really long and cumbersome, if you have a lot of GPs to check for.  Perhaps something like this would work for you.

Uppercase( Regex( :TEST_NAME, "io_(gp_\d+)", "\1", IGNORE CASE ) );

It look for "io_" followed by "gp_" and any number of digits.  "IGNORE CASE" is on so that "GP" will also satisfy the Regex.  If that's not the behavior you want, just leave off that part.

txnelson
Super User

Re: Search specific name by IF

I worked this code up and it seems to work, and works pretty fast

names default to here(1);

dt=current data table();

dt << new column("PIN_NAME", character);

theRows = dt << get rows where( contains(:TEST_NAME,"_gp_"));

for each( {fndRow,i}, theRows,
	:PIN_NAME[fndRow] = word(1,uppercase(substr(:TEST_NAME[fndRow],contains(:TEST_NAME[fndRow],"_gp_")+1)),"[]")
);
Jim