cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
hogi
Level XII

robust column reference

After a few minutes coding with JSL, users wonder about strange objects like :sex in 

 

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

x = :sex

 

 

Expert users have some experience with it and use it all the time.
But what is :sex ? Is  dt:sex  more robust? Where do I find a documentation ...

Let's collect and share some insights.

my 5 cents:

6 REPLIES 6
hogi
Level XII

Re: robust column reference

In JSL, :sex can be used in two ways: 

1. as a reference to a column

:sex << get name

 

 ... and 2 : to retrieve the entry of the current row

new column("s", formula(:sex) )

which is the short form of 

new column("s", formula (:sex[row()])

// slightly shorter:

new column("s", formula (:sex[])



To create such an object from a string, one has 2 options:

column name = "sex";
col= Column (column name); // for the first one

val = As Column (column name) // for the second one


There is also a very cool trick, which I learned from @jthi :

the universal column reference = Name Expr(As Column(column name))

it can be use like :sex for both approaches.

Column() vs As Column() vs datable:column vs dt:As name("column") 

hogi
Level XII

Re: robust column reference

:sex looks fragile - what happens when the user changes the column name?

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

x = Name Expr(As column("name"));
Show(Name Expr(x)); // : name 

:name << set name("The name");

Show(Name Expr(x)); // :The name

wow, great!

hogi
Level XII

Re: robust column reference

And when the user changes the data table?

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

x = Name Expr(As column("name"));
Show(Name Expr(x));
x[1] = "Marker";

:name << set name("The name");

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

Show(Name Expr(x));
Show(x[1])

The innocent :sex even knows which data table it actually belongs to. Wonderful!

hogi
Level XII

Re: robust column reference

Now, let's go from 1 column to 2 columns:

myList1 = {:name,:sex,:age};


A very complicated and useless way to create this list is:

myList2 = Eval List({Name Expr(As column(:name)),Name Expr(As column(:sex)),Name Expr(As column(:age))});

 

hogi_0-1758660151284.png

 

hogi
Level XII

Re: robust column reference

useless?

you already notice the big difference when you try to execute the above code without a 

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


Then the second line will return

hogi_1-1758660245484.png

but the fist line doesn't have this "issue".

"issue", let's sum up and try:

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

myList1 = {:name,:sex,:age};

myList2 = Eval List({Name Expr(As column(:name)),Name Expr(As column(:sex)),Name Expr(As column(:age))});

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

dt << Select Rows( [1] ) << Delete Rows;

Show(Eval List(myList1), Eval List(myList2));

hogi_2-1758660353341.png

 

Show(Type(myList1[1]), Type(myList2[1]))

provides more insight:

hogi_3-1758660470227.png


The simple list contains "Names", the complicated list contains "Columns".
When a name is evaluated, JMP follows a specific rule to determine what this name means:
 https://www.jmp.com/support/help/en/19.0/#page/jmp/rules-for-resolving-names.shtml 

If the name refers to a column, JMP will access the column in the current data table (dt2).

 

For a  Column, JMP just has to go to THE data table ("dt") and access the column.
Very robust!

hogi
Level XII

Re: robust column reference

Name Expr(As column(:col))

is soooo cool!

Recommended Articles