cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
KinKame
Level IV

column number

Hi all,

how can I get the column number in a table?

as below header 3 is column # 3 in the table ...

Header 1Header 2Header 3
1 ACCEPTED SOLUTION

Accepted Solutions
msharp
Super User (Alumni)

Re: column number

I'm sorry, but this post went from bad coding practices to worse coding practices.  As the saying goes, there are multiple ways to skin a cat, and there are multiple ways to write code to perform the same function.  However, what is the best way?  Generally, we will want to consider at LEAST the three main factors speed, length of code, and error proneness.  For this, reason,


number_of_columns = n items( dt << Get Column Names());

is really poor programming.  What you should use is:

number_of_columns = ncols(dt);

The obvious is it's shorter, but it's also faster.  The former has to create a list of all the column names, and then count them.  The latter will simply grab the table variable.

Next, column3 = column(dt, 3); should never be used.  The first reason is b/c column3 is a poor name.  I'm not going to go into why, but you can read this post for clarification What's in a Name? Anti-Patterns to a Hard Problem.  The second reason, is b/c of the additional step you are introducing. Why do:

column_nr = Contains(dt << get column names(), Column("height"));

column = column(dt, column_nr);

When you could just:

column = Column(dt, "height");

Again, the latter is shorter and faster.

View solution in original post

6 REPLIES 6
KinKame
Level IV

Re: column number

without using a loop for !!!

ms
Super User (Alumni) ms
Super User (Alumni)

Re: column number

Below is an approach using Contains that does what (I think) you're asking for:

dt = Open("$SAMPLE_DATA/Big Class.jmp");

//Works!

column_nr = Contains(dt << get column names(string), "height");

column_nr = Contains(dt << get column names(), Column("height"));

column_nr = Contains(dt << get column names(), expr(height));


//Works not

column_nr = Contains(dt << get column names(), height);

Nate_Riordan
Staff (Retired)

Re: column number

I like where you're heading with this MS.  I might start out with a


          dt = Current Data Table();


And if llupo0's question were how many columns are there your could be modified it to look like this:

          number_of_columns = n items( dt << Get Column Names());

If ultimately interested, llupo0 could then refer to the columns by number:

          column3 = column(dt, 3);

     value1_column3 = column3[1];

msharp
Super User (Alumni)

Re: column number

I'm sorry, but this post went from bad coding practices to worse coding practices.  As the saying goes, there are multiple ways to skin a cat, and there are multiple ways to write code to perform the same function.  However, what is the best way?  Generally, we will want to consider at LEAST the three main factors speed, length of code, and error proneness.  For this, reason,


number_of_columns = n items( dt << Get Column Names());

is really poor programming.  What you should use is:

number_of_columns = ncols(dt);

The obvious is it's shorter, but it's also faster.  The former has to create a list of all the column names, and then count them.  The latter will simply grab the table variable.

Next, column3 = column(dt, 3); should never be used.  The first reason is b/c column3 is a poor name.  I'm not going to go into why, but you can read this post for clarification What's in a Name? Anti-Patterns to a Hard Problem.  The second reason, is b/c of the additional step you are introducing. Why do:

column_nr = Contains(dt << get column names(), Column("height"));

column = column(dt, column_nr);

When you could just:

column = Column(dt, "height");

Again, the latter is shorter and faster.

KinKame
Level IV

Re: column number

nice ... and yes it works perfectly thank you

msharp
Super User (Alumni)

Re: column number

Could you explain a little on why you want this? 99 times out of a 100, this value is not beneficial and there's a better coding methodology that should be implemented.  This is especially true since to get this number you'd either need to know the Column Reference or Column Name, which are both more valuable.