<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: create a loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55204#M31209</link>
    <description>&lt;P&gt;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 =&amp;gt; Window =&amp;gt; Log) and the version of JMP you are using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;listoftables = {"Untitled 5", "Untitled 4", "Untitled 3"};

// Loop through each of the data tables and add the column

For( i = 1, i &amp;lt;= N Items( listoftables ), i++,

	dt = Data Table( listoftables[i] );
	dt&amp;lt;&amp;lt;new column("Sequence", numeric, formula(Row()));
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Apr 2018 09:46:32 GMT</pubDate>
    <dc:creator>shoffmeister</dc:creator>
    <dc:date>2018-04-18T09:46:32Z</dc:date>
    <item>
      <title>create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55202#M31207</link>
      <description>&lt;P&gt;I&amp;nbsp;want to add a column with the row numbers to multiple data tables using the following script:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt&amp;lt;&amp;lt;new column("Sequence", numeric, formula(Row()));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm stil new in scripting. How can I process all my data tables automatically ? I have this line of info which could&amp;nbsp;work, but it seems that there is a problem for locating&amp;nbsp;"Data Table"...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;= N Items( dtList ), i++,
dt = Data Table( dtList[i] );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 17:38:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55202#M31207</guid>
      <dc:creator>paul_apicella</dc:creator>
      <dc:date>2018-04-18T17:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55204#M31209</link>
      <description>&lt;P&gt;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 =&amp;gt; Window =&amp;gt; Log) and the version of JMP you are using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;listoftables = {"Untitled 5", "Untitled 4", "Untitled 3"};

// Loop through each of the data tables and add the column

For( i = 1, i &amp;lt;= N Items( listoftables ), i++,

	dt = Data Table( listoftables[i] );
	dt&amp;lt;&amp;lt;new column("Sequence", numeric, formula(Row()));
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 09:46:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55204#M31209</guid>
      <dc:creator>shoffmeister</dc:creator>
      <dc:date>2018-04-18T09:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55205#M31210</link>
      <description>&lt;P&gt;Ya!&amp;nbsp;it works well !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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" ? &lt;SPAN&gt;That takes a lot of time… how do you advise me to proceed?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks in advance&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 10:51:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55205#M31210</guid>
      <dc:creator>paul_apicella</dc:creator>
      <dc:date>2018-04-18T10:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55206#M31211</link>
      <description>&lt;P&gt;There are commands to pick a directory and to list all files in that directory.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would recommend to check for each file if it is a JMP file (e.g. by checking the file-ending).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dirpath = Pick Directory();
allfiles = Files in Directory(dirpath);

allfiles&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Apr 2018 11:30:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55206#M31211</guid>
      <dc:creator>shoffmeister</dc:creator>
      <dc:date>2018-04-18T11:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55207#M31212</link>
      <description>&lt;P&gt;Apparently. It's not right yet. There's something wrong with the "For" line ?&lt;/P&gt;&lt;PRE class=" language-jsl"&gt;&lt;CODE class="  language-jsl"&gt;&lt;SPAN class="token comment"&gt;dirpath = Pick Directory();&lt;BR /&gt;allfiles = Files in Directory (dirpath);&lt;BR /&gt;all files&lt;BR /&gt;&lt;BR /&gt;// Loop through each of the files and add the column&lt;/SPAN&gt;

&lt;SPAN class="token function"&gt;For&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; i &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; i &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;N Items&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; dirpath &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;++&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;

	dt &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;Data Table&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; dispath&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	dt&lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;new column&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"Sequence"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; numeric&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; formula&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;Row&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 12:29:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55207#M31212</guid>
      <dc:creator>paul_apicella</dc:creator>
      <dc:date>2018-04-18T12:29:19Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55208#M31213</link>
      <description>&lt;P&gt;You have "dispath" in your loop.&amp;nbsp; Change it to "dirpath"&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 13:14:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55208#M31213</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2018-04-18T13:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55209#M31214</link>
      <description>I 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:&lt;BR /&gt;Unexpected "For". Perhaps there's a missing ; or .&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Apr 2018 13:24:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55209#M31214</guid>
      <dc:creator>paul_apicella</dc:creator>
      <dc:date>2018-04-18T13:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55212#M31216</link>
      <description>&lt;P&gt;You've got a number of issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've confused &lt;FONT face="andale mono,times"&gt;dirpath&lt;/FONT&gt; and &lt;FONT face="andale mono,times"&gt;allfiles&lt;/FONT&gt; and used them incorrectly in your code. The result of &lt;A href="https://www.jmp.com/support/help/14/file-functions.shtml#5041137" target="_self"&gt;Pick Directory()&lt;/A&gt;, &lt;FONT face="andale mono,times"&gt;dirpath&lt;/FONT&gt;, 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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result of &lt;A href="https://www.jmp.com/support/help/14/file-functions.shtml#4951479" target="_self"&gt;Files in Directory()&lt;/A&gt;, &lt;FONT face="andale mono,times"&gt;allfiles&lt;/FONT&gt;, will be the list of files in the directory. That's what you should be looping over.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll need to &lt;A href="https://www.jmp.com/support/help/14/file-functions.shtml#4959899" target="_self"&gt;Open()&lt;/A&gt; the data tables before you can add the new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the &lt;FONT face="andale mono,times"&gt;allfiles&lt;/FONT&gt; list is only the filenames, you'll need to concatenate the directory, &lt;FONT face="andale mono,times"&gt;dirpath&lt;/FONT&gt;, in the argument to Open().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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 &lt;A href="https://www.jmp.com/support/help/14/character-functions-2.shtml#2491416" target="_self"&gt;Word()&lt;/A&gt; function. The -1 argument asks for the last word in the string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dirpath = Pick Directory();
allfiles = Files In Directory( dirpath );

// Loop through each of the files and add the column
Show( allfiles );

For( i = 1, i &amp;lt;= N Items( allfiles ), i++,
	If( Word( -1, allfiles[i], "." ) == "jmp",
		dt = Open( dirpath || allfiles[i] );
		dt &amp;lt;&amp;lt; New Column( "Sequence", numeric, formula( Row() ) );
	)
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check out the &lt;A href="https://www.jmp.com/support/help/14/get-started-2.shtml#713746" target="_self"&gt;Get Started&lt;/A&gt; section of the &lt;A href="https://www.jmp.com/support/help/14/data-tables-2.shtml#" target="_self"&gt;Data Tables&lt;/A&gt; chapter of the &lt;A href="https://www.jmp.com/support/help/14/scripting-guide.shtml#" target="_self"&gt;Scripting Guide&lt;/A&gt; for an introduction to working with data tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 13:57:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55212#M31216</guid>
      <dc:creator>Jeff_Perkinson</dc:creator>
      <dc:date>2018-04-18T13:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55219#M31220</link>
      <description>Thank you very much Jeff. That's perfect.&lt;BR /&gt;I absolutely must find the time to take a closer look at the reading of the scripting guide. It will help me.</description>
      <pubDate>Wed, 18 Apr 2018 14:55:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/create-a-loop/m-p/55219#M31220</guid>
      <dc:creator>paul_apicella</dc:creator>
      <dc:date>2018-04-18T14:55:56Z</dc:date>
    </item>
  </channel>
</rss>

