create a loopI want to add a column with the row numbers to multiple data tables using the following script:
dt<<NEW column=""></NEW>
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] );
Re: create a loopThank you very much Jeff. That's perfect.
I absolutely must find the time to take a closer look at the reading of the scripting guide. It will help me.Re: create a loopYou'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.
Re: create a loopI have made an error in my message while copying the script but the I worked with the correct version and yet I still have an error message with the Debug option:
Unexpected "For". Perhaps there's a missing ; or .
Re: create a loopYou have "dispath" in your loop. Change it to "dirpath"
Re: create a loopApparently. It's not right yet. There's something wrong with the "For" line ?
<SPAN class="token comment">dirpath = Pick Directory();<BR />allfiles = Files in Directory (dirpath);<BR />all files<BR /><BR />// Loop through each of the files and add the column</SPAN>
<SPAN class="token function">For</SPAN><SPAN class="token punctuation">(</SPAN> i <SPAN class="token operator">=</SPAN> <SPAN class="token number">1</SPAN><SPAN class="token punctuation">,</SPAN> i <SPAN class="token operator"><=</SPAN> <SPAN class="token function">N Items</SPAN><SPAN class="token punctuation">(</SPAN> dirpath <SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">,</SPAN> i<SPAN class="token operator">++</SPAN><SPAN class="token punctuation">,</SPAN>
dt <SPAN class="token operator">=</SPAN> <SPAN class="token function">Data Table</SPAN><SPAN class="token punctuation">(</SPAN> dispath<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN> <SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
dt<SPAN class="token operator"><</SPAN><SPAN class="token operator"><</SPAN><SPAN class="token function">new column</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token string">"Sequence"</SPAN><SPAN class="token punctuation">,</SPAN> numeric<SPAN class="token punctuation">,</SPAN> formula<SPAN class="token punctuation">(</SPAN><SPAN class="token function">Row</SPAN><SPAN class="token punctuation">(</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
Re: create a loopThere 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
Re: create a loopYa! 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
Re: create a loopThe 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=""></NEW>