cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
%3CLINGO-SUB%20id%3D%22lingo-sub-777763%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3EActualizar%20una%20matriz%20asociativa%20que%20es%20una%20variable%20global%20de%20clase%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777763%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EHola%2C%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3ETengo%20una%20variable%20de%20clase%20que%20es%20una%20matriz%20asociativa.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edefine%20class%20(%0A%22class%20name%22%2C%0A%0Aass_array%20%20%3D%20associative%20array%20()%3B%0A%0Ameth1%20%3D%20method(%7Bx%2Cy%7D%2C%0Afor(i%20%3D%201%2C%20n%20%26lt%3B%3D%20n%20items(x)%2Ci%2B%2B%0Axi%20%3D%20x%5Bi%5D%3B%0Ayi%20%3D%20y%5Bi%5D%3B%0Atry(%0Aass_array%20%20%26lt%3B%26lt%3B%20insert(xi%2C%20Insert(eval%20list(ass_array%5Beval(xi)%5D)%2C%20eval(yi)))%2C%0Aass_array%20%20%26lt%3B%26lt%3B%20insert(xi%2C%20List(eval(yi)))%0A)%3B%0A%0A)%3B%0A%0A%0A)%3B%0A%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Epero%20cuando%20llamo%20a%20esta%20clase%20para%20actualizar%20la%20matriz%20asociativa%2C%20la%20matriz%20todav%C3%ADa%20est%C3%A1%20vac%C3%ADa.%20%C2%BFAlguna%20idea%3F%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3ETambi%C3%A9n%20vale%20la%20pena%20se%C3%B1alar%20que%20prob%C3%A9%20esta%20sintaxis%20externa%20a%20la%20clase%20para%20actualizar%20una%20matriz%20asociativa%20y%20actu%C3%B3%20como%20se%20esperaba.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-777763%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CLINGO-LABEL%3EAutomatizaci%C3%B3n%20y%20secuencias%20de%20comandos%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eventanas%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777786%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Actualizar%20un%20arreglo%20asociativo%20que%20es%20una%20variable%20global%20de%20clase%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777786%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3ECreo%20que%20es%20posible%20que%20desees%20agregar%20un%20m%C3%A9todo%20de%20inicializaci%C3%B3n.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0A%0ADefine%20Class(%22class%20name%22%2C%0A%09ass_array%20%3D%20Associative%20Array()%3B%0A%09%0A%09_init_%20%3D%20Method(%7B%7D%2C%0A%09%09this%3Aass_array%20%3D%20ass_array%3B%0A%09)%3B%0A%09%0A%09meth1%20%3D%20Method(%7Bx%2C%20y%7D%2C%0A%09%09ass_array%20%3D%20Associative%20Array()%3B%0A%0A%09%09For(i%20%3D%201%2C%20i%20%26lt%3B%3D%20N%20Items(x)%2C%20i%2B%2B%2C%0A%09%09%09xi%20%3D%20x%5Bi%5D%3B%0A%09%09%09yi%20%3D%20y%5Bi%5D%3B%0A%09%09%09Try(%0A%09%09%09%09ass_array%20%26lt%3B%26lt%3B%20Insert(xi%2C%20Insert(Eval%20List(ass_array%5BEval(xi)%5D)%2C%20Eval(yi)))%2C%0A%09%09%09%09ass_array%20%26lt%3B%26lt%3B%20Insert(xi%2C%20List(Eval(yi)))%0A%09%09%09)%3B%0A%09%09)%3B%0A%09)%3B%0A)%3B%0A%0Acn%20%3D%20New%20Object(%22class%20name%22)%3B%0A%0Ashow(cn%3Aass_array)%3B%0Acn%3Ameth1(%7B%22a%22%2C%20%22b%22%7D%2C%20%7B%22c%22%2C%20%22d%22%7D)%3B%0Ashow(cn%3Aass_array)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Eesto%20no%20es%20necesario%20y%20no%20estoy%20seguro%20de%20si%20deber%C3%ADa%20usarse%20(pero%20se%20usa%20aqu%C3%AD%20%3CA%20href%3D%22https%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2Fen%2F18.0%2F%23page%2Fjmp%2Fadvanced-classes.shtml%23ww761769%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2Fen%2F18.0%2F%23page%2Fjmp%2Fadvanced-classes.%20shtml%23ww761769%3C%2FA%3E%20)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777788%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Actualizar%20un%20arreglo%20asociativo%20que%20es%20una%20variable%20global%20de%20clase%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777788%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHola%2C%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3Eentonces%20tengo%20un%20m%C3%A9todo%20de%20inicio.%20No%20saqu%C3%A9%20todo%20el%20flujo%20de%20trabajo%20de%20la%20clase%20por%20pereza.%20No%20pens%C3%A9%20que%20fuera%20necesario%20solucionar%20el%20problema.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777790%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Actualizar%20un%20arreglo%20asociativo%20que%20es%20una%20variable%20global%20de%20clase%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777790%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EPero%20crear%20una%20instancia%20de%20la%20variable%20con%20%3CEM%3Ethis%3Aarray%3C%2FEM%3E%20parec%C3%ADa%20haber%20funcionado.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EPor%20qu%C3%A9%20esto%20hace%20una%20diferencia%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777814%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Actualizar%20un%20arreglo%20asociativo%20que%20es%20una%20variable%20global%20de%20clase%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777814%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FHow-to-use-Define-Class%2Fm-p%2F274410%23U274410%22%20class%3D%22lia-mention-container-editor-message%20lia-img-icon-blog-thread%20lia-fa-icon%20lia-fa-blog%20lia-fa-thread%20lia-fa%22%20target%3D%22_blank%22%3EC%C3%B3mo%20utilizar%20Definir%20clase%3C%2FA%3E%20podr%C3%ADa%20dar%20alguna%20explicaci%C3%B3n%3C%2FP%3E%3C%2FLINGO-BODY%3E
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