cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
miguello
Level VI

How to read table into list?

Let's say I have a table "dt" where column names are meaningless.

And I have a table "dt_source" with two columns - one column meaningless name and the other has corresponding meaningful name.

I'm trying to write a script that would rename columns from meaningless to meaningful.

Columns might be different, so I'm going to create a master table "dt_source" with corresponding values.

Now I have a script ready that takes meaningless and leaningful names from a list withing the script and renames colums for each pair if such meaningless name exists.

I want to be able to take those corresponding pairs not from typed in list:

name_in={"col1", "col2"};

name_out={"Age", "Height")

but from a table.

How do I read dt_name_source into two lists: name_in and name_out?

So far I have this:

 

Names Default To Here(1);

 

Try(dt_source = Data Table("NameChanging.jmp"), Throw("Trying to open data table"));

 

If( Not( Is Scriptable( dt_source ) ),

 

dt_source = Open( "NameChanging.jmp" ), dt_source = Data Table("NameChanging.jmp");

 

);

 

Current Data Table(dt_source);

 

name_in=List();

 

name_out=List();

 

col_name_in=Column("name_in");

 

col_name_out=Column("name_out");

 

i=0;

 

For Each Row(name_in[i]=col_name_in[]; name_out[i]=col_name_out[]; i++);

 

But then it doesn't like it:

Subscript problem{1} in access or evaluation of 'Assign' , name_in =  /*###*/col_name_in[] /*###*/

If I try just a variable

For Each Row(name_in=col_name_in[]; name_out=col_name_out[]);

Then it's OK, but it's just one variable taking different values as the cycle goes.

And I need a list.

Can somebody give an idea how to handle this?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to read table into list?

I believe the following script will get the values into the lists you need:

Names Default To Here( 1 );

If( Try( dt_source << get name, "" ) != "NameChanging",

       dt_source = Open( "NameChanging.jmp" )

);

name_in = dt_source:name_in << get values;

name_out = dt_source:name_out << get values;

Jim

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: How to read table into list?

I believe the following script will get the values into the lists you need:

Names Default To Here( 1 );

If( Try( dt_source << get name, "" ) != "NameChanging",

       dt_source = Open( "NameChanging.jmp" )

);

name_in = dt_source:name_in << get values;

name_out = dt_source:name_out << get values;

Jim
miguello
Level VI

Re: How to read table into list?

Jim,

Thanks a lot for the reply. It works just fine.

However, I found that my renaming code does not work if there are no columns with one of the names from name_in (and there will often be columns in table that do not have ALL names from NameChanging.jmp table).

So far I have this:

for(i=1, i<=N Items(name_out), i++,

show(i); Try(col=Column(name_in)<<Set Name(name_out), Throw("NO COLUMN FOUND");

show(col)); show(i)

)

But cycle breaks after fifrst iteration when it does not finds the first name from name_in in the table.

miguello
Level VI

Re: How to read table into list?

Jim,

This seems to work fine for now. Let me know if this is a correct way of doing things:

for(i=1, i<=N Items(name_out), i++,

show(i); If( !Is Missing( Is Scriptable( Try( Column( name_in ) ) ) ), col=Column(name_in)<<Set Name(name_out);

show(col); show(i))

) ;

txnelson
Super User

Re: How to read table into list?

I would probably do things a little differently:

Names Default to Here( 1 );

// Pointer to the data table to be changed

dt = data table("The name of the table to be changed");

for(i=1, i<=N Items(name_out), i++,

    If(try(column(dt, name_in) << get name, "") != "", column( dt, name_in) << Set Name(name_out));

);

Jim