cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-777763%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3EMettre%20%C3%A0%20jour%20un%20tableau%20associatif%20qui%20est%20une%20variable%20globale%20de%20classe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777763%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EBonjour%2C%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EJ'ai%20une%20variable%20de%20classe%20qui%20est%20un%20tableau%20associatif%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%3Emais%20lorsque%20j'appelle%20cette%20classe%20pour%20mettre%20%C3%A0%20jour%20le%20tableau%20associatif%2C%20le%20tableau%20est%20toujours%20vide.%20Des%20pens%C3%A9es%3F%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EIl%20convient%20%C3%A9galement%20de%20noter%20que%20j'ai%20test%C3%A9%20cette%20syntaxe%20externe%20%C3%A0%20la%20classe%20pour%20mettre%20%C3%A0%20jour%20un%20tableau%20associatif%20et%20qu'elle%20a%20agi%20comme%20pr%C3%A9vu.%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%3EAutomatisation%20et%20scripts%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eles%20fen%C3%AAtres%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%20%3A%20Mettre%20%C3%A0%20jour%20un%20tableau%20associatif%20qui%20est%20une%20variable%20globale%20de%20classe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777786%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EJe%20pense%20que%20vous%20voudrez%20peut-%C3%AAtre%20ajouter%20une%20m%C3%A9thode%20d'initialisation%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%3Ece%20n'est%20pas%20n%C3%A9cessaire%20et%20je%20ne%20suis%20pas%20s%C3%BBr%20qu'il%20doive%20m%C3%AAme%20%C3%AAtre%20utilis%C3%A9%20(mais%20il%20est%20utilis%C3%A9%20ici%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%20%3A%20Mettre%20%C3%A0%20jour%20un%20tableau%20associatif%20qui%20est%20une%20variable%20globale%20de%20classe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777788%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EBonjour%2C%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3Edonc%2C%20j'ai%20une%20m%C3%A9thode%20init.%20Je%20n'ai%20pas%20mis%20tout%20le%20flux%20de%20travail%20de%20la%20classe%20par%20paresse.%20Je%20ne%20pensais%20pas%20qu'il%20%C3%A9tait%20n%C3%A9cessaire%20de%20r%C3%A9soudre%20le%20probl%C3%A8me.%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%20%3A%20Mettre%20%C3%A0%20jour%20un%20tableau%20associatif%20qui%20est%20une%20variable%20globale%20de%20classe%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777790%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EMais%2C%20instancier%20la%20variable%20avec%20%3CEM%3Ethis%3Aarray%3C%2FEM%3E%20semble%20avoir%20fonctionn%C3%A9.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EPourquoi%20cela%20fait-il%20une%20diff%C3%A9rence%20%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%20%3A%20Mettre%20%C3%A0%20jour%20un%20tableau%20associatif%20qui%20est%20une%20variable%20globale%20de%20classe%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%3EComment%20utiliser%20Define%20Class%3C%2FA%3E%20pourrait%20donner%20quelques%20explications%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