Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
user8421
Level I

Saving incremented variable names in For loop to a list

I am trying to 

1) Open a series of files that are listed in the list Unique_Files

2) If the file doesn't exist, then a dialog box opens up with a message indicating the file exists

3) If the file does exist, then open the file in JMP

4) If the file does exist, then add the data table reference name to the list UniqueTables

 

Every other part of my script seems to be working except being able to create a list of the data table reference name.  The way my script is written below results in the list

 

UniqueTables = { DataTable("filename1"), Data Table("filename2"),....}

Instead I want the script to result in the list as

 

UniqueTables = My_Table_1, My_Table_2, My_Table_3,...

 

Thanks in advance.

 

N Items (Unique_Files);

UniqueTables = { };

For ( i = 1, i <= N, i ++,
     If(
          File Exists( "C:\PretendFilePath\" ||Unique_Files[i]|| ".csv"),
          Eval(
               Substitute(
                    Expr( 
                         MyTab = Open( "C:\PretendFilePath\" ||Unique_Files[i]|| ".csv");
                         Insert Into( UniqueTables, MyTab) 
                    ),
                    Expr( MyTab),
                    Parse( My_Table_" ||Char(i))
               )
          ),
          New Window ("File Not Found",
               << Modal,
               V List Box(
                    Text Box( Unique_Files[i]|| " does not exist in folder."),
                    Button Box( "OK")
               )
          )
     )
);
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: Saving incremented variable names in For loop to a list

All you need to do is to retrieve the table name from the data table reference, so change your reference to "Mytab" to:

 Insert Into( UniqueTables, MyTab << get name)

All of the messages for the various elements in JSL are documented in the Scripting Guide

     Help==>Scripting Guide

Jim

View solution in original post

9 REPLIES 9
Highlighted
txnelson
Super User

Re: Saving incremented variable names in For loop to a list

All you need to do is to retrieve the table name from the data table reference, so change your reference to "Mytab" to:

 Insert Into( UniqueTables, MyTab << get name)

All of the messages for the various elements in JSL are documented in the Scripting Guide

     Help==>Scripting Guide

Jim

View solution in original post

Highlighted
user8421
Level I

Re: Saving incremented variable names in For loop to a list

Hi Jim, 

 

Unfortunately I'm still not getting the desired result because I wasn't clear in how I explained my situation.

 

I should have specified that the Unique_Files list is the file name of the CSVs stored in the folder location.

 

 

Unique_Files = {"FileName1", "FileName2", "FileName3", etc...}


Using the << Get Name function results in the UniqueTables list as

 

 

UniqueTables = {"FileName1", "FileName2", "FileName3", etc...}


When I'm looking for 

UniqueTables = {My_Table_1, My_Table_2, My_Table_3, etc...}


Once I get the UniqueTables list together in that format my plan was to concatenate some of those tables together and close the source tables that were concatenated.  I only know how to do this with the table reference name of My_Table_1.  Is there a way to concatenate tables from their file name of "FileName1"?  If there is, then I can bypass this issue altogether.

 

Thanks in advance!  You have been very helpful.

Highlighted
vince_faller
Super User

Re: Saving incremented variable names in For loop to a list

Check out what an XY problem is, it will definitely help us help you.  

 

But other than that.  If all you're trying to do is concat all the JMP files in a folder together. 

 

Names default to here( 1 );
// this is just creating an example set don't worry about it.  
dt = open("$SAMPLE_DATA\Big Class.jmp");
dir = convert file path("$DESKTOP\Example\");
Create Directory(dir);

for(i=1, i<=6, i++, 
	path = convert file path(char(i)||".jmp", base(dir));
	dt << Save As(path);
);


// this is actually what you'll want to run
dir = convert file path("$DESKTOP\Example\");
// find the files in that directory
files = files in directory(dir);
// create a base table
dt_final = New Table("Concatted");

for(i=1, i<=nitems(files), i++, 
	
	dt = open(convert file path(files[i], base(dir)));
	dt_final << Concatenate(dt, append to first table(1));
	close(dt, no save);
)
Vince Faller - Predictum
Highlighted
vince_faller
Super User

Re: Saving incremented variable names in For loop to a list

Can I ask why you want to do this?  Doing this makes the script very hard to support because you can't find variable names. I fix scripts once the owners can't understand their own code anymore and this is always something I'm running into.  

What does giving multiple variables give you that a list doesn't?  

Because it seems unnecessary to have 

 

UniqueTables[1] = My_Table_1 = DataTable("filename1");

 

 

Vince Faller - Predictum
Highlighted
txnelson
Super User

Re: Saving incremented variable names in For loop to a list

You appear to have some lack of knowledge about what the various structures are actually giving you.  I strongly suggest that you take the time to read the Scripting Guide.  But, I also think the below might clear up some issues with your current code.  Take a look at my annotated code below, and see if it adds some light to the issues you are having.

names default to here(1);

dt1=open("$SAMPLE_DATA/big class.jmp");
dt2=open("$SAMPLE_DATA/big class families.jmp");

// Here is what the value of dt1 is
show(dt1);
// From the log
// dt1 = DataTable("big class");
// Therefore the value of dt2 is,
show(dt2);
//  dt2 = DataTable("big class families");

// The structure of the Concatenate Platform syntax is:
// data table("data table name") << Concatenate( data table("data table2 name"), data table("data table2 name") );

// That would mean that the following will concatenate our two files
data table( "big class" ) << Concatenate( data table( "big class families" ) );

// It also means, that since we have variables dt1, which was shown to equal Data Table( "big class" ), and 
// dt2 which has the value of Data Table( "big class families" )
// the above concatenate statement can be written
dt1 << Concatenate( dt2 );

// Here is another way to do this

UniqueTables = {};
MyTab = open("$SAMPLE_DATA/big class.jmp");

insert into( UniqueTables, MyTab );

MyTab = open("$SAMPLE_DATA/big class families.jmp");

insert into( UniqueTables, MyTab );

Show( UniqueTables );
// returns
// UniqueTables = {DataTable("big class"), DataTable("big class families")};

// So now the Concatenate statement can be written
UniqueTables[1] << Concatenate( UniqueTables[2] );
Jim
Highlighted
jimloughlin
Level III

Re: Saving incremented variable names in For loop to a list

On the last line I get the following error:

 

Unexpected "UniqueTables". Perhaps there is a missing ";" or ",".
Line 42 Column 1: ►UniqueTables[1] << Concatenate( UniqueTables[2] );
The remaining text that was ignored was
UniqueTables[1]<<Concatenate(UniqueTables[2]);

Highlighted
txnelson
Super User

Re: Saving incremented variable names in For loop to a list

Some how a period "." ended up after

Show( UniqueTables );.

so remove it and it will work fine.....I will correct the previous example for future references. 

Jim
Highlighted
jimloughlin
Level III

Re: Saving incremented variable names in For loop to a list

That period was hard to see !!!!

Highlighted
txnelson
Super User

Re: Saving incremented variable names in For loop to a list

yup

Jim
Article Labels

    There are no labels assigned to this post.