cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
vmontes
Level I

Loop for opening files, changing a column name and saving in the original format

Hello all,

 

I've tried to assemble a script for opening a series of files (.dx) and changing the text from one of the columns on each file. I manage to make it work for one or two files individually but if I try a set of four an infinite loop begins. Can anybody please help? There is a chance that the issue is with the files but I would like to vet the script first since I'm a novice at jsl. Ideally, I would like to save the files not as jmp but as dx also. 

 

Files = Pick File( "Select File(s)", "G:\Retention Compliant\Research\Analytical\Projects\IR database", {"All Files|*"}, 1, 0, "", "multiple" );
For( i = 1, i <= N Items( Files ), i++,
	Try( dt = Open( Files[i], "text" ) );
	wait( 0 );
For( n = 1, n <= N Items (Files), n++, 
colList = dt << get column names( character, string );
For( i = 1, i <= N Items( colList ), i++,
	selRows = dt << get rows where(
		Column( dt, colList[i] )[Row()] == "##YUNITS= % Transmittance" | Column( dt, colList[i] )[Row()] == "##YUNITS= % T"
	);
	If( N Rows( selRows ) > 0,
		Column( dt, colList[i] )[selRows] = "##YUNITS= % T";
	);
);
) );
4 REPLIES 4
Craige_Hales
Super User

Re: Loop for opening files, changing a column name and saving in the original format

1) reformat your JSL using the editor right-click popup. You'll be able to see the nesting better.

2) you are reusing the i variable in the inner loop. That's causing the loop.

3) remove the try() wrapper, or make it do something when it catches an error.

 

Craige
vmontes
Level I

Re: Loop for opening files, changing a column name and saving in the original format

Thanks Craige, it works now with all files and no errors! Any ideas how to save in the original format (.dx) and preferably preserving the file name?

Craige_Hales
Super User

Re: Loop for opening files, changing a column name and saving in the original format

Not sure what dx is, but you can save with .csv extension and probably be close. 

Craige
txnelson
Super User

Re: Loop for opening files, changing a column name and saving in the original format

I have annotated the script below with the issues I found.  I am surprised you can read in a .dx file.  JMP certainly does not support the writing out of .dx files(as far as I know).

Files = Pick File(
	"Select File(s)",
	"G:\Retention Compliant\Research\Analytical\Projects\IR database",
	{"All Files|*"},
	1,
	0,
	"",
	"multiple"
);
For( i = 1, i <= N Items( Files ), i++,
	// Open one file
	Try( dt = Open( Files[i], "text" ) );
	Wait( 0 );
	// Loop from 1 to the number of files (even though only 1 file has been opened)
	For( n = 1, n <= N Items( Files ), n++,
	    // Here you will be reading the column names in for the first file over and over
		colList = dt << get column names( character, string );
		// Loop using "i" as the index across all of the columns....in the first file
		// since that is all we have read in.....but since you are using "i", you will
		// be creating an infinate loop, since you are using "i" for the first loop
		// and you will be interferring with the assumed stepping for the outter loop
		For( i = 1, i <= N Items( colList ), i++,
			selRows = dt << get rows where(
				// You are not in a formula, so the Row() function will not provide a predictable result
				Column( dt, colList[i] )[Row()] == "##YUNITS= % Transmittance" | Column( dt, colList[i] )[Row()] ==
				"##YUNITS= % T"
			);
			If( N Rows( selRows ) > 0,
				Column( dt, colList[i] )[selRows] = "##YUNITS= % T"
			);
		);
	);
);
Jim