cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
doctorfizz
Level II

Update an Associative array that is a class global variable

Hello,

 

I have a class variable that is an associative array

 

define class (
"class name",

ass_array  = associative array ();

meth1 = method({x,y},
for(i = 1, n <= n items(x),i++
xi = x[i];
yi = y[i];
try(
ass_array  << insert(xi, Insert(eval list(ass_array[eval(xi)]), eval(yi))),
ass_array  << insert(xi, List(eval(yi)))
);

);


);

);

but when I call this class to update the associative array, the array is still empty. Any thoughts? 

 

It is also worth noting that I tested this syntax external to the class to update an associative array and it acted as expected. 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Update an Associative array that is a class global variable

I think you might want to add initialization method

Names Default To Here(1);


Define Class("class name",
	ass_array = Associative Array();
	
	_init_ = Method({},
		this:ass_array = ass_array;
	);
	
	meth1 = Method({x, y},
		ass_array = Associative Array();

		For(i = 1, i <= N Items(x), i++,
			xi = x[i];
			yi = y[i];
			Try(
				ass_array << Insert(xi, Insert(Eval List(ass_array[Eval(xi)]), Eval(yi))),
				ass_array << Insert(xi, List(Eval(yi)))
			);
		);
	);
);

cn = New Object("class name");

show(cn:ass_array);
cn:meth1({"a", "b"}, {"c", "d"});
show(cn:ass_array);

this isn't necessary and I'm not sure if it should even be used (but it is used here https://www.jmp.com/support/help/en/18.0/#page/jmp/advanced-classes.shtml#ww761769)

-Jarmo

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Update an Associative array that is a class global variable

I think you might want to add initialization method

Names Default To Here(1);


Define Class("class name",
	ass_array = Associative Array();
	
	_init_ = Method({},
		this:ass_array = ass_array;
	);
	
	meth1 = Method({x, y},
		ass_array = Associative Array();

		For(i = 1, i <= N Items(x), i++,
			xi = x[i];
			yi = y[i];
			Try(
				ass_array << Insert(xi, Insert(Eval List(ass_array[Eval(xi)]), Eval(yi))),
				ass_array << Insert(xi, List(Eval(yi)))
			);
		);
	);
);

cn = New Object("class name");

show(cn:ass_array);
cn:meth1({"a", "b"}, {"c", "d"});
show(cn:ass_array);

this isn't necessary and I'm not sure if it should even be used (but it is used here https://www.jmp.com/support/help/en/18.0/#page/jmp/advanced-classes.shtml#ww761769)

-Jarmo
doctorfizz
Level II

Re: Update an Associative array that is a class global variable

Hello,

 

so, I have an init method. I did not put the entire workflow of the class out of laziness. I did not think it was necessary to troubleshoot the problem. 

doctorfizz
Level II

Re: Update an Associative array that is a class global variable

But, instantiating the variable with the this:array seemed to have worked.

 

Why does this make a difference? 

jthi
Super User

Re: Update an Associative array that is a class global variable

How to use Define Class might give some explanation

-Jarmo