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

recode multiple columns name with regex

 

Hi all!

I am trying to apply a regex replacement to a Recode Column function in JSL so that the script will go over all columns and apply the regex to all of them, if applicable.

 

This is the code I've been testing:

 

Names Default To Here(1);

dt = Current Data Table();

//Create list of columns to be recoded 
ColN = dt << Get Column Names();

// Iterate over the list of columns and recode each one
For Each({listitem}, ColN,
	dt << Recode Column(
		As Column(listitem),
		{Regex(_rcNow, "erm", "testtest", GLOBALREPLACE)},
		Target Column(Column(listitem))
	);
);

I am getting this error: 

Name Unresolved: LocalTime in access or evaluation of 'LocalTime' , LocalTime/*###*/

LocalTime is the first column  out of 13 columns in my dataset.

 

I would appreciate any help!

3 REPLIES 3
Lavik17
Level II

Re: recode multiple columns name with regex

I just realized that I used Recode Column and not a function that will rename the column name... 

Is there a dedicated function to recoding a column name? is there another way to accomplish it?

 

jthi
Super User

Re: recode multiple columns name with regex

There Recode Column Names... which to my knowledge works in similar manner but it won't get recorded to Action Recorder and you cannot get script out of it. There also doesn't seem to be mention of it in Scripting Index.

jthi_0-1652849266550.png

You might have to create loop and use for example Regex() and << Set Name to columns.

Names Default To Here(1);

dt = New Table("test",
	new column("erm")
);

wait(1); // for demo

For Each({col_name}, dt << Get Column Names("String"),
	new_col_name = Regex(col_name, "erm", "testtest", GLOBALREPLACE);
	Column(dt, col_name) << Set Name(new_col_name);
);

 

-Jarmo
txnelson
Super User

Re: recode multiple columns name with regex

A list when specified will not execute any elements within the list.  Therefore, the coder needs to force the list to be fully expanded before it is passed to JMP.  Below is one way of handling the issue

Names Default To Here( 1 );

dt = Current Data Table();

//Create list of columns to be recoded 
ColN = dt << Get Column Names(string);

// Iterate over the list of columns and recode each one
For Each( {listitem}, ColN,show(listitem);
	Eval(
		Substitute(
				Expr(
					dt << Recode Column(
						As Column( listitem ),
						{Regex( __rcNow__, "erm", "testtest", GLOBALREPLACE )},
						Target Column( Column( listitem ) )
					)
				),
			Expr( __rcNow__ ), listitem
		)
	)
);
Jim