Loop for opening files, changing a column name and saving in the original formatHello 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";
);
);
) );Automation and ScriptingRe: Loop for opening files, changing a column name and saving in the original formatI 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"
);
);
);
);Re: Loop for opening files, changing a column name and saving in the original formatNot sure what dx is, but you can save with .csv extension and probably be close.
Re: Loop for opening files, changing a column name and saving in the original formatThanks 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?
Re: Loop for opening files, changing a column name and saving in the original format1) 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.