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
robot
Level VI

Associative Array as Table Variable

Is it possible to create an Associative Array as a Table Variable and then use stored values in a column formula?

 

I have a number of related variables I would like to edit as part of an ongoing analysis.  These variables will then be used like a Match function in a column formula.

 

For example:

  1. Create table variable counts and set value to ["a" => 10, "b" => 3, "c" => 1].
  2. counts is then referenced by a column formula, like :counts[:col].  Where :col is a Character column with values "a", "b", or "c".

Is this possible?  I am using JMP 18.2.1.

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Associative Array as Table Variable

I think this does what you asked:

New Table( "Untitled",
	Add Rows( 3 ),
	New Table Variable( "abc", "[\!"a\!"=>1, \!"b\!"=>2, \!"c\!"=>4]" ),
	New Column( "Column 1",
		Numeric,
		"Nominal",
		Format( "Best", 9 ),
		Formula(
			c_abc = As Constant( Eval( Parse( :abc ) ) );
			c_abc["c"];
		),
		Set Selected
	)
)

Note the table variable is just text, so it gets parsed and evaluated. The AsConstant function makes it only parse/eval once for the entire column's evaluation, so it won't be too inefficient.

As an alternative, you could have a separate table variable for each element of the associative array. It might be easier to edit, but it won't create an isolated namespace like the associative array.

Another alternative is to use custom column properties if the names are going to be column names. They might be harder to find and edit. Unlike the text-only table variable, they also hold expressions:

dt=New Table( "Untitled",
	New Column( "Column 1",
		Set Property( "xxxxx", ["a"=>42,"adfasdfasdf"=>77] )
	)
);

(dt:column1<<getproperty("xxxxx"))["a"] // 42

 

Craige

View solution in original post

3 REPLIES 3
Craige_Hales
Super User

Re: Associative Array as Table Variable

I think this does what you asked:

New Table( "Untitled",
	Add Rows( 3 ),
	New Table Variable( "abc", "[\!"a\!"=>1, \!"b\!"=>2, \!"c\!"=>4]" ),
	New Column( "Column 1",
		Numeric,
		"Nominal",
		Format( "Best", 9 ),
		Formula(
			c_abc = As Constant( Eval( Parse( :abc ) ) );
			c_abc["c"];
		),
		Set Selected
	)
)

Note the table variable is just text, so it gets parsed and evaluated. The AsConstant function makes it only parse/eval once for the entire column's evaluation, so it won't be too inefficient.

As an alternative, you could have a separate table variable for each element of the associative array. It might be easier to edit, but it won't create an isolated namespace like the associative array.

Another alternative is to use custom column properties if the names are going to be column names. They might be harder to find and edit. Unlike the text-only table variable, they also hold expressions:

dt=New Table( "Untitled",
	New Column( "Column 1",
		Set Property( "xxxxx", ["a"=>42,"adfasdfasdf"=>77] )
	)
);

(dt:column1<<getproperty("xxxxx"))["a"] // 42

 

Craige
robot
Level VI

Re: Associative Array as Table Variable

Thanks @Craige_Hales , your solution works great.

 

Thanks @jthi , I upvoted this suggestion.

jthi
Super User

Re: Associative Array as Table Variable

Craige did provide a good options how you can do this. Here is a wish list item hoping for more complicated data types for table variables Let us use more complicated data types for table variables than just text and numbers 

-Jarmo

Recommended Articles