cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
shaira
Level IV

How to convert the name of list into a string?

Hi,

I have this script which checks if the list has duplicate elements, and if it does, it sends error message.

ColAgain=Function({main}, {duplicate},
					duplicate={};
					duplicate_r={};
					unique={};
					For (i=1,i<=Nitems(main),i++,
						If (Nrows(Loc(unique,main[i])) == 0,
							InsertInto (unique,main[i]),
							InsertInto (duplicate_r,main[i])
							);
						);
					For (i=1,i<=Nitems(duplicate_r),i++,
						If (Nrows(Loc(duplicate,duplicate_r[i])) == 0,
							InsertInto (duplicate,duplicate_r[i])
							);

						);
					if (Nitems(duplicate)>0,
						Write("ERROR. "||Char(main)||" has duplicates.");
						Caption("ERROR. "||Char(main)||" has duplicates. Check log.");
						For (i=1, i<=Nitems(duplicate), i++,
							 Show(duplicate[i]);
							);
					Throw();
						);
					duplicate;
				 );

Notice that I have this line:

Caption("ERROR. "||Char(main)||" has duplicates. Check log.");

What I want to appear is this:

"ERROR. GroupVar has duplicates. Check log."

However, this appears instead:

"ERROR. {"Element1", "Element2, "ElementN"} has duplicates. Check log."

Any ideas? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Phil_Brown
Super User (Alumni)

Re: How to convert the name of list into a string?

Hi @shaira

I assume you want the VARIABLE name that is supplied as the function argument, to be displayed in the caption box? If so, this may work:

 

ColAgain = Function( {main},
	{duplicate},
	duplicate = {};
	duplicate_r = {};
	unique = {};
	For( i = 1, i <= N Items( main ), i++,
		If( N Rows( Loc( unique, main[i] ) ) == 0,
			Insert Into( unique, main[i] ),
			Insert Into( duplicate_r, main[i] )
		)
	);
	For( i = 1, i <= N Items( duplicate_r ), i++,
		If( N Rows( Loc( duplicate, duplicate_r[i] ) ) == 0,
			Insert Into( duplicate, duplicate_r[i] )
		);

	);
	If( N Items( duplicate ) > 0,
		Write( "ERROR. " || Char( main ) || " has duplicates." );
		Caption( "ERROR. " || Char( nameExpr(main) ) || " has duplicates. Check log." );
		For( i = 1, i <= N Items( duplicate ), i++,
			Show( duplicate[i] )
		);
		Throw();
	);
	duplicate;
);

groupVar = {1,2,3,3,5};
ColAgain( expr(groupVar) ); 

 

Note the use of Name Expr() in the Caption statement. Also, when calling the function, you have to wrap the argument with the keyword Expr()

PDB

View solution in original post

2 REPLIES 2
Phil_Brown
Super User (Alumni)

Re: How to convert the name of list into a string?

Hi @shaira

I assume you want the VARIABLE name that is supplied as the function argument, to be displayed in the caption box? If so, this may work:

 

ColAgain = Function( {main},
	{duplicate},
	duplicate = {};
	duplicate_r = {};
	unique = {};
	For( i = 1, i <= N Items( main ), i++,
		If( N Rows( Loc( unique, main[i] ) ) == 0,
			Insert Into( unique, main[i] ),
			Insert Into( duplicate_r, main[i] )
		)
	);
	For( i = 1, i <= N Items( duplicate_r ), i++,
		If( N Rows( Loc( duplicate, duplicate_r[i] ) ) == 0,
			Insert Into( duplicate, duplicate_r[i] )
		);

	);
	If( N Items( duplicate ) > 0,
		Write( "ERROR. " || Char( main ) || " has duplicates." );
		Caption( "ERROR. " || Char( nameExpr(main) ) || " has duplicates. Check log." );
		For( i = 1, i <= N Items( duplicate ), i++,
			Show( duplicate[i] )
		);
		Throw();
	);
	duplicate;
);

groupVar = {1,2,3,3,5};
ColAgain( expr(groupVar) ); 

 

Note the use of Name Expr() in the Caption statement. Also, when calling the function, you have to wrap the argument with the keyword Expr()

PDB
shaira
Level IV

Re: How to convert the name of list into a string?

Thanks! It works perfectly well.