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

Find and Replace all instances of a specific character with another character but leave the rest untouched

Heyho!

 

I am currently trying to replace all instances of "." with "," in a script, since I am in the EU and that is our decimal. Have seen some solutions to about the same problem that I have but they do not seem to work or I am just too stupid to use them for my case.

Any help is appreciated!

I will attach a picture of my data.

 

data.png

 

Kind regards,

Lien

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

Even though I'm from EU I always use "." as my decimal separator, so my example will be using "," as "wrong" decimal. Starting table:

jthi_0-1659368979989.png

Recode Column 1 to capture the script

jthi_1-1659368993796.png

jthi_2-1659369003220.png

jthi_3-1659369009393.png

jthi_4-1659369019962.png

From Enhanced Log

jthi_5-1659369041392.png

 

And finally script it

Names Default To Here(1);

dt = New Table("Untitled 3",
	Add Rows(2),
	New Column("Column 1", Character, "Nominal", Set Values({"1.1", "1.2", "1.2E-3"})),
	New Column("Column 2", Character, "Nominal", Set Values({"2,1", "2,3", "1.2E3"}))
);
dt << Begin Data Update;

For Each({col_name}, dt << Get Column Names("String"),
	dt << Recode Column(
		AsColumn(col_name),
		{Substitute(_rcNow, ",", ".")},
		Update Properties(1),
		Target Column(As Column(col_name))
	);
	Column(dt, col_name) << Set Data Type("Numeric");
	Column(dt, col_name) << Set Modeling Type("Continuous");
);

dt << End Data Update;	
-Jarmo

View solution in original post

5 REPLIES 5
jthi
Super User

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

Even though I'm from EU I always use "." as my decimal separator, so my example will be using "," as "wrong" decimal. Starting table:

jthi_0-1659368979989.png

Recode Column 1 to capture the script

jthi_1-1659368993796.png

jthi_2-1659369003220.png

jthi_3-1659369009393.png

jthi_4-1659369019962.png

From Enhanced Log

jthi_5-1659369041392.png

 

And finally script it

Names Default To Here(1);

dt = New Table("Untitled 3",
	Add Rows(2),
	New Column("Column 1", Character, "Nominal", Set Values({"1.1", "1.2", "1.2E-3"})),
	New Column("Column 2", Character, "Nominal", Set Values({"2,1", "2,3", "1.2E3"}))
);
dt << Begin Data Update;

For Each({col_name}, dt << Get Column Names("String"),
	dt << Recode Column(
		AsColumn(col_name),
		{Substitute(_rcNow, ",", ".")},
		Update Properties(1),
		Target Column(As Column(col_name))
	);
	Column(dt, col_name) << Set Data Type("Numeric");
	Column(dt, col_name) << Set Modeling Type("Continuous");
);

dt << End Data Update;	
-Jarmo
Lien
Level II

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

Hey!

 

Thanks a bunch for your answer! After modifying the code a bit I got it to work for me! 

Now I'd only need to set the Column Format to "Engineering". Do you know how I could add that to my script?

 

Thanks in advance!

 

 

jthi
Super User

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

Easiest way is to let JMP script it for you and then use that script yourself.

1. Change column Format to Engineering on some column and press apply

jthi_0-1659425382607.png

2. If you have JMP16 and enhanced log enabled you will see the script needed there (just fix references)

jthi_1-1659425418625.png

 

3. If not, there are still few ways to get the script:

4. Use data tables red triangle option and use Copy Table Script (No Data)

jthi_2-1659425480897.png

5. Find the column you changed to Engineering

jthi_3-1659425527983.png

6. Just try getting the Format part from the script and using in on different column

 

:height << Format("Engineering", 6);

7. If it works, great. If it doesn't it is time to open Scripting Index (it might be good idea to search for it in scripting index anyway).

 

8. Search for Format and from the long list of different JMP Objects look for Data Table and Column Scripting. This will help you narrow down the options

jthi_4-1659425643416.png

 

-Jarmo
txnelson
Super User

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

My suggestion is to use the Find/Replace dialog.  It allows you to search the entire data table and replace the "," with the "." in one click.

Give the following data table

txnelson_0-1659373331491.png

there are several values that contain commas.

Either type

     CNTL/F

or go to

     Edit=>Search=>Find

the following window will open

txnelson_1-1659373585995.png

Simply fill in the string to find and the string to replace it with

txnelson_2-1659373659921.png

and then click on Replace All, and the data table will be searched and all commas will be replaced with periods

txnelson_3-1659373770071.png

And the data table will be changed

txnelson_4-1659373808253.png

 

Jim
Lien
Level II

Re: Find and Replace all instances of a specific character with another character but leave the rest untouched

Hello!

 

Thanks for your answer! Unfortunately I'd need it in a script.I already knew of the search and replace function of JMP but I needed it automated.

The other answer from jthi already helped me out here but thanks anyways!

 

Kind regards,

Lien