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
mhenry26
Level I

Many column names, search for part of column name string

Hello,

I have been at this for a day or two now and cannot seem to reach a solution. I have a large amount of columns names and wish to write a script that will "look" at each column name and identify if the first couple characters are the characters I have specified. The list of column names have a series of, varying, first 3 characters, for example: "XX." "YY." "ZZ.". What my overall intentions are to search through these columsn and group columns in groups that have :  "XX." "YY." "ZZ." etc..

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz
Super User

Re: Many column names, search for part of column name string

This should get you started.

// Create a table to test things out on

dt = New Table( "Test", Add Rows( 3 ),

    New Column( "XX1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "XX2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) )

);

// List of column names for the table

col_names = dt << get column names(string);

// Empty list to be populated with found columns

found_list = {};

// String we're looking for

looking_for = "YY";

// Loop over all column names, looking for ones that start with "YY"

for (i = 1, i <= nitems(col_names), i++,

    if (starts with(col_names[i], looking_for),

        insertinto(found_list, col_names[i]);

    );

);

print(found_list);

View solution in original post

4 REPLIES 4
pmroz
Super User

Re: Many column names, search for part of column name string

This should get you started.

// Create a table to test things out on

dt = New Table( "Test", Add Rows( 3 ),

    New Column( "XX1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "XX2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) )

);

// List of column names for the table

col_names = dt << get column names(string);

// Empty list to be populated with found columns

found_list = {};

// String we're looking for

looking_for = "YY";

// Loop over all column names, looking for ones that start with "YY"

for (i = 1, i <= nitems(col_names), i++,

    if (starts with(col_names[i], looking_for),

        insertinto(found_list, col_names[i]);

    );

);

print(found_list);

mhenry26
Level I

Re: Many column names, search for part of column name string

PMroz,

Thank you very much for your help. I tested it and it seems to be generating the list that I wanted.  I was not aware of the "starts with" or "insertinto" commands, i am pretty new to JSL. I was looking for some kind of 'InString' character function, which does not seem to exist.  Another question, now that my column names are stored in a list/row vector, can I now store them into a group? Just to make the user interface on the JMP table a little easier to look at? Thanks again for the help.

pmroz
Super User

Re: Many column names, search for part of column name string

The instring equivalent in JSL is contains.  The menu option Help > Scripting Index is very helpful, btw.

This code groups each set of XXs and YYs into separate groups.

// Create a table to test things out on

dt = New Table( "Test", Add Rows( 3 ),

    New Column( "XX1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "XX2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY1", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) ),

    New Column( "YY2", Numeric, Continuous, Format( "Best", 12 ), Set Values( [1, 2, 3] ) )

);

// List of column names for the table

col_names = dt << get column names(string);

// String we're looking for

group_list = {"XX", "YY"};

// Loop over column groups

for (i = 1, i <= nitems(group_list), i++,

// Empty list to be populated with found columns

    found_list = {};

    one_group = group_list[i];

// Loop over all column names, looking for ones that start with "YY"

    for (k = 1, k <= nitems(col_names), k++,

        if (starts with(col_names[k], one_group),

            insertinto(found_list, col_names[k]);

        );

    );

   

    if (nitems(found_list) > 0,

        dt << group columns(one_group, found_list);

    );

);

mhenry26
Level I

Re: Many column names, search for part of column name string

Thank you again, it seems to work on my test data table! I was trying to use the Word function with a delimiter and generate a list from there,  but your method is a lot better. Also, thank you for the tip about the 'Scripting Index' that is very helpful. I have been using the Scripting Guide, but the index is great. I can see I have a so much to learn and I appreciate all of your help to get me started.