Subscribe Bookmark RSS Feed

How to convert the name of list into a string?

shaira

Contributor

Joined:

May 4, 2017

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

Joined:

Mar 20, 2012

Solution

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
2 REPLIES
Phil_Brown

Super User

Joined:

Mar 20, 2012

Solution

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

Contributor

Joined:

May 4, 2017

Thanks! It works perfectly well.