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

How can I easily retrieve the column "position" in a large table?

Hello JMP Community,

While writing quick and simple scripts with For loops, I usually enter the starting and ending columns manually (i.e. For i=start, i <=end, i++) to allow for the analysis of different ranges of variables.

Is there a simple way to retrieve the position of a given column to use as either starting or ending column? I use the rather awkward method of selecting all columns up to the desired column but it seems highly inefficient. Another method I have used is to create a new table where the column names are stacked in rows and use the row numbers as positions. 

I assume that there is a simpler method that has escaped me so far.

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How can I easily retrieve the column "position" in a large table?

What I use is:

position=Loc(dt<<get column names("string"),"theDesiredColumn")[1];
Jim

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: How can I easily retrieve the column "position" in a large table?

What I use is:

position=Loc(dt<<get column names("string"),"theDesiredColumn")[1];
Jim
Craige_Hales
Super User

Re: How can I easily retrieve the column "position" in a large table?

I'd forgotten about the "string" argument! 

Craige
Craige_Hales
Super User

Re: How can I easily retrieve the column "position" in a large table?

Try this:

dt=open("$sample_data/big class.jmp");
lookup=associativearray((dt<<getcolumnreference)<<getname,1::ncols(dt));
show(lookup["age"],lookup["height"]);

lookup["age"] = 2;
lookup["height"] = 4;

The associative array must be rebuilt if the columns are moved/deleted/added in the table. dt<<GetColumnReference returns a list of columns; sending that list the <<GetName message produces a list of the string names of the columns that can be used as the keys to the associative array. The matrix 1::ncols(dt) makes the values for the associative array. The lookups are then done with the string name of the column.

There is also a dt<<GetColumnNames message that returns the names (not strings) of the columns. Those names are harder to work with than the list of columns from dt<<GetColumnReference.  The column<<GetName message returns a string (in spite of its name). In the example, <<GetName is sent to a { list of columns } which causes the message to be sent to each item in the list and a new list is built from the results.

Craige