cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Sign-in to the JMP Community will be unavailable intermittently Dec. 6-7 due to a system update. Thank you for your understanding!
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.
  • JMP 19 is here! Learn more about the new features.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
shaira
Level IV

User-Defined Function: How to pass output variable value to another variable

Hi, I am making a code that validates user input. The user input are two lists, B and C. The output is list A, which contains all the items that are contained in both B and C. Here is the code:

//CHECK IF USER INPUT MAKES SENSE 
Caption("Checking input data...");
col_name_list = dt << get column names( string );

//Check if there are duplicate or missing inputs
//Create a User-Defined Function
ColCheck=Function({main, second}, {duplicate},
	(duplicate={};  Show (duplicate);
	For (i=1, i<=Nitems(main), i++,
		 //Check if there are missing columns from user input
		 If ( !Contains( col_name_list, main[i] ),
			  Write("WARNING! Column " || main[i] ||" does not exists. Creating data column.");
			  Caption("WARNING! Column " || main[i] ||" does not exists. Creating data column.");
			  Wait( 1 );
			  script1 = "dt<<New Column(\!"" || main[i] ||"\!", numeric, continuous);";
			  script1 = Eval( Parse( script1 ) );
			 );
		 //Check if there are overlap columns between two lists
		 For (j=1, j<=Nitems(second), j++,
		 	  If ( second[j] == main[i],
				   duplicate=Insert(duplicate,second[j]); Show (duplicate);
				 );
			 );	
		);
)
);
//Quick test to show if function is working	
A={}; Show(A);
B={"one", "two", "three"};
C={"four", "five", "three", "two"};
A=ColCheck(B,C);
Show(A);

Here is the output:

A = {};
duplicate = {};
duplicate = {"two"};
duplicate = {"two", "three"};
A = .;

The functions works, as seen from the values of "duplicate" variable.  The problem is this: the duplicate content cannot be passed to variable "A". Is there anything wrong with my code?

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: User-Defined Function: How to pass output variable value to another variable

What is passed back from a JMP function or Format is the last value processed, therefore if you add "duplicate" as the last item, it works great.

ColCheck = Function( {main, second},
	{duplicate},
	duplicate = {};
	Show( duplicate );
	For( i = 1, i <= N Items( main ), i++, 
		 //Check if there are missing columns from user input
		If( !Contains( col_name_list, main[i] ),
			Write( "WARNING! Column " || main[i] || " does not exists. Creating data column." );
			Caption( "WARNING! Column " || main[i] || " does not exists. Creating data column." );
			Wait( 1 );
			script1 = "dt<<New Column(\!"" || main[i] || "\!", numeric, continuous);";
			script1 = Eval( Parse( script1 ) );
		);
		 //Check if there are overlap columns between two lists
		For( j = 1, j <= N Items( second ), j++,
			If( second[j] == main[i],
				duplicate = Insert( duplicate, second[j] );
				Show( duplicate );
			)
		);
	);
	duplicate;
);
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: User-Defined Function: How to pass output variable value to another variable

What is passed back from a JMP function or Format is the last value processed, therefore if you add "duplicate" as the last item, it works great.

ColCheck = Function( {main, second},
	{duplicate},
	duplicate = {};
	Show( duplicate );
	For( i = 1, i <= N Items( main ), i++, 
		 //Check if there are missing columns from user input
		If( !Contains( col_name_list, main[i] ),
			Write( "WARNING! Column " || main[i] || " does not exists. Creating data column." );
			Caption( "WARNING! Column " || main[i] || " does not exists. Creating data column." );
			Wait( 1 );
			script1 = "dt<<New Column(\!"" || main[i] || "\!", numeric, continuous);";
			script1 = Eval( Parse( script1 ) );
		);
		 //Check if there are overlap columns between two lists
		For( j = 1, j <= N Items( second ), j++,
			If( second[j] == main[i],
				duplicate = Insert( duplicate, second[j] );
				Show( duplicate );
			)
		);
	);
	duplicate;
);
Jim
shaira
Level IV

Re: User-Defined Function: How to pass output variable value to another variable

Thanks Jim. It's working now.

 

Best,

Shaira

Recommended Articles