BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
miguello

Community Trekker

Joined:

Jan 27, 2016

How to CORRECTLY name a table?

All,

 

I know how to set a name to a table. But sometimes, if that name already exists (by chance or from left after previous runs of the script), the name instead of setting ot "My Table" would set to is "My Table 2". How do I correctly set it to exactly what I want it to be set to?

I'm looking for a snippet of code that does this - checks if table with this name exasts, if yes, closes or renames old one, and then names this one with exact name I asked for.

I tried Try() function but I probably don't understand entirely how it works. I tried some complex if() statements that gets the list of all the tables and iterates through names etc. It's too long and on big number of tables slows down significantly. 

 

So, I was wondering, what is the CORRECT, conventional way of naming a table so that you're sure the table is named exactly like that?

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

Re: How to CORRECTLY name a table?

I am sure that different Community Members may have a variety of different ways to attack this issue.  Here is one that seems to be a pretty simple approach.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );

xt = dt << subset( selected rows( 0 ), selected columns( 0 ) );

setName = Function( {tdt, Name},
	{tdt, Name},
	Try( Data Table( Name ) << set name( Name || "_Old" ) );
	tdt << set name( name );
);

setName( xt, "big class" );
Jim
5 REPLIES 5
ms

Super User

Joined:

Jun 23, 2011

Re: How to CORRECTLY name a table?

I am not aware of a conventional way (other than avoiding names already in use). I agree that it can be difficult after prolonged uptime to keep track of all open windows.

 

Here's one JSL idea to do what you describe (tested in JMP 14). Seems to be quite efficient. To always be globally available, the function can be added as a Custom function() by a start-up script that is run automatically everytime JMP is launched.

// Function for renaming data table if desired name is used by another open table
rename_if_exists = Function({s},
    open_tables = Get Window List(Type("data tables")) << Get Window Title();
    exists = Loc(open_tables, s)[1];
    If(exists,
        Data Table(open_tables[exists]) << set name(open_tables[exists] || "_old")
    );
    s;
);

// Apply...
dt = New Table(rename_if_exists("untitled 28972"), add rows(10));

 

miguello

Community Trekker

Joined:

Jan 27, 2016

Re: How to CORRECTLY name a table?

As I mentioned I would like to avoid pulling list of all tables opened. When producing hundreds of summary tables in one script's run, it takes a lot of time.
txnelson

Super User

Joined:

Jun 22, 2012

Solution

Re: How to CORRECTLY name a table?

I am sure that different Community Members may have a variety of different ways to attack this issue.  Here is one that seems to be a pretty simple approach.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );

xt = dt << subset( selected rows( 0 ), selected columns( 0 ) );

setName = Function( {tdt, Name},
	{tdt, Name},
	Try( Data Table( Name ) << set name( Name || "_Old" ) );
	tdt << set name( name );
);

setName( xt, "big class" );
Jim
miguello

Community Trekker

Joined:

Jan 27, 2016

Re: How to CORRECTLY name a table?

Yes, that's how I tried to approach it. I probably do not understand fully how Try() works. Let me try your way.
miguello

Community Trekker

Joined:

Jan 27, 2016

Re: How to CORRECTLY name a table?

The only issue with this approach that I see is that... well, basically you're trying to make Set Name safer to use by writing a new function instead. But in that function you use Set Name again, which is prone to the same issues you're trying to solve... :) But it shouldn't matter for your immediate table and therefore the script itself, so we're good here.
At this point I'd probably just close old tables without saving them.