Choose Language Hide Translation Bar
Highlighted
johnmoore
Level IV

Help with custom function

hash = Function( {StringList},
	{StringListItem,HashString},
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);

This is a hash function that I use to create unique ID's.  The function currently takes a list as it's argument.  I would to have it take one or more arguments separated by commas.  I am hoping to add it to custom functions and make it available to other users.  Thought that would be easier for users if it accepted multiple arguments separated by commas like the Sum() function.

 

Any thoughts?

 

Many thanks,

 

John

 

3 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
txnelson
Super User

Re: Help with custom function

Here is a method using your code that would allow for the entry of some large preset maximum, but variable list of input parameters for your hash function

hash = Function( {a, b = "", c = "", d = ""}, // e="",f=""...…...z="")
	//{StringListItem,HashString},
	StringList = {};
	string# = 1;
	varList = {"a", "b", "c", "d"}; // .........."z"
	
	While( Eval( Parse( varlist[string#] ) ) != "",
		Insert Into( stringList, Eval( Parse( varlist[string#] ) ) );
		string# = string# + 1;
	);
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);
Jim

View solution in original post

Highlighted
vince_faller
Super User

Re: Help with custom function

It might not be exactly what you're looking for but you could just evallist if it's really just a list of strings.  

 

Infinite unknown arguments are easiest with a list.  

 

Names default to here(1);
hash = Function( {StringList},
	{StringListItem,HashString},
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);

different_hash = function({StringList}, 
	StringList = Evallist(StringList);
	Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);

x = "2";
y = "a";
z = "Expr(b)";
b = "another string";

l = {x, y, z};
show(hash(l) == different_hash(l));
Vince Faller - Predictum

View solution in original post

Highlighted
gzmorgan0
Super User

Re: Help with custom function

@johnmoore ,

 

This is another variation of @vince_faller and @txnelson 's solutions.

Names default to here(1);
hash = Function( {val1, val2=., val3=., val4=., val5=., val6=., val7=., val8=., val9=., val10=.},
	{i, StringListItem,HashString},
	if(IsList(val1), StringList = EvalList(val1),
		StringList={};
		For(i=1, i<=10,i++,
			Eval( Parse(EvalInsert("If( !IsMissing(val^i^) & IsString(val^i^), InsertInto(StringList, val^i^))" ) ) )
		);
	);
	//Show(StringList);
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);


x = "2";
y = "a";
z = "Expr(b)";
b = "another string";

hash(x,y,z);
l = {x, y, z};
show(hash(l), hash(x,y,z), hash( list(x,y,z) )  );  //produces the same result 

It would take some documentation/training for your users, that up to 10 string arguments can be entered or use a list if more than 10.

View solution in original post

4 REPLIES 4
Highlighted
txnelson
Super User

Re: Help with custom function

Here is a method using your code that would allow for the entry of some large preset maximum, but variable list of input parameters for your hash function

hash = Function( {a, b = "", c = "", d = ""}, // e="",f=""...…...z="")
	//{StringListItem,HashString},
	StringList = {};
	string# = 1;
	varList = {"a", "b", "c", "d"}; // .........."z"
	
	While( Eval( Parse( varlist[string#] ) ) != "",
		Insert Into( stringList, Eval( Parse( varlist[string#] ) ) );
		string# = string# + 1;
	);
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);
Jim

View solution in original post

Highlighted
vince_faller
Super User

Re: Help with custom function

It might not be exactly what you're looking for but you could just evallist if it's really just a list of strings.  

 

Infinite unknown arguments are easiest with a list.  

 

Names default to here(1);
hash = Function( {StringList},
	{StringListItem,HashString},
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);

different_hash = function({StringList}, 
	StringList = Evallist(StringList);
	Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);

x = "2";
y = "a";
z = "Expr(b)";
b = "another string";

l = {x, y, z};
show(hash(l) == different_hash(l));
Vince Faller - Predictum

View solution in original post

Highlighted
gzmorgan0
Super User

Re: Help with custom function

@johnmoore ,

 

This is another variation of @vince_faller and @txnelson 's solutions.

Names default to here(1);
hash = Function( {val1, val2=., val3=., val4=., val5=., val6=., val7=., val8=., val9=., val10=.},
	{i, StringListItem,HashString},
	if(IsList(val1), StringList = EvalList(val1),
		StringList={};
		For(i=1, i<=10,i++,
			Eval( Parse(EvalInsert("If( !IsMissing(val^i^) & IsString(val^i^), InsertInto(StringList, val^i^))" ) ) )
		);
	);
	//Show(StringList);
	For( StringListItem = 1, StringListItem <= Length( StringList ), StringListItem++,
		StringList[StringListItem] = Char( Eval( Stringlist[StringListItem] ) )
	);
	HashString = Hex( Blob MD5( Char To Blob( Concat Items( StringList ) ) ) );
);


x = "2";
y = "a";
z = "Expr(b)";
b = "another string";

hash(x,y,z);
l = {x, y, z};
show(hash(l), hash(x,y,z), hash( list(x,y,z) )  );  //produces the same result 

It would take some documentation/training for your users, that up to 10 string arguments can be entered or use a list if more than 10.

View solution in original post

Highlighted
johnmoore
Level IV

Re: Help with custom function

Many thanks for your solution!
Article Labels

    There are no labels assigned to this post.