- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
create a loop
I want to add a column with the row numbers to multiple data tables using the following script:
dt<<new column("Sequence", numeric, formula(Row()));
I'm stil new in scripting. How can I process all my data tables automatically ? I have this line of info which could work, but it seems that there is a problem for locating "Data Table"...
Names Default To Here( 1 );
// Create a list of data tables to be updated
dtList = {"Table1", "Table2", etc...};
// Loop through each of the data tables and add the column
For( i = 1, i <= N Items( dtList ), i++,
dt = Data Table( dtList[i] );
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
You've got a number of issues.
You've confused dirpath and allfiles and used them incorrectly in your code. The result of Pick Directory(), dirpath, will be a string that is the path to the directory that the user chooses. You've tried to use it in the indexing of your For() loop below as if it had the list of files.
The result of Files in Directory(), allfiles, will be the list of files in the directory. That's what you should be looping over.
You'll need to Open() the data tables before you can add the new column.
Since the allfiles list is only the filenames, you'll need to concatenate the directory, dirpath, in the argument to Open().
Finally, you should make sure that you're only trying to open JMP data tables by checking the extension on the files. In the example code below, I do that with the Word() function. The -1 argument asks for the last word in the string.
dirpath = Pick Directory();
allfiles = Files In Directory( dirpath );
// Loop through each of the files and add the column
Show( allfiles );
For( i = 1, i <= N Items( allfiles ), i++,
If( Word( -1, allfiles[i], "." ) == "jmp",
dt = Open( dirpath || allfiles[i] );
dt << New Column( "Sequence", numeric, formula( Row() ) );
)
);
Check out the Get Started section of the Data Tables chapter of the Scripting Guide for an introduction to working with data tables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
The following script works for me. If it does not for you, it would be helpful to post the content of your log (Main Menu => Window => Log) and the version of JMP you are using.
listoftables = {"Untitled 5", "Untitled 4", "Untitled 3"};
// Loop through each of the data tables and add the column
For( i = 1, i <= N Items( listoftables ), i++,
dt = Data Table( listoftables[i] );
dt<<new column("Sequence", numeric, formula(Row()));
);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
Ya! it works well !
Is it possible to request - at the start of the script - the opening of all data tables that are in a folder in order to run the script thereafter ? Should the title of each file be indicated one after the other in the "listoftables" ? That takes a lot of time… how do you advise me to proceed?
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
There are commands to pick a directory and to list all files in that directory.
I would recommend to check for each file if it is a JMP file (e.g. by checking the file-ending).
dirpath = Pick Directory();
allfiles = Files in Directory(dirpath);
allfiles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
Apparently. It's not right yet. There's something wrong with the "For" line ?
dirpath = Pick Directory();allfiles = Files in Directory (dirpath);all files// Loop through each of the files and add the column
For( i = 1, i <= N Items( dirpath ), i++,
dt = Data Table( dispath[i] );
dt<<new column("Sequence", numeric, formula(Row()));
);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
You have "dispath" in your loop. Change it to "dirpath"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
Unexpected "For". Perhaps there's a missing ; or .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
You've got a number of issues.
You've confused dirpath and allfiles and used them incorrectly in your code. The result of Pick Directory(), dirpath, will be a string that is the path to the directory that the user chooses. You've tried to use it in the indexing of your For() loop below as if it had the list of files.
The result of Files in Directory(), allfiles, will be the list of files in the directory. That's what you should be looping over.
You'll need to Open() the data tables before you can add the new column.
Since the allfiles list is only the filenames, you'll need to concatenate the directory, dirpath, in the argument to Open().
Finally, you should make sure that you're only trying to open JMP data tables by checking the extension on the files. In the example code below, I do that with the Word() function. The -1 argument asks for the last word in the string.
dirpath = Pick Directory();
allfiles = Files In Directory( dirpath );
// Loop through each of the files and add the column
Show( allfiles );
For( i = 1, i <= N Items( allfiles ), i++,
If( Word( -1, allfiles[i], "." ) == "jmp",
dt = Open( dirpath || allfiles[i] );
dt << New Column( "Sequence", numeric, formula( Row() ) );
)
);
Check out the Get Started section of the Data Tables chapter of the Scripting Guide for an introduction to working with data tables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: create a loop
I absolutely must find the time to take a closer look at the reading of the scripting guide. It will help me.