Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Mon Jun 14, 2004 3:11 pm   

Searching one list with another
 
I'm trying to make a custom function that will scan one list looking for one or more instances of something in another list. Here's an example:

#VAR list1 {blah1|blah2|blah3}
#VAR list2 {blah1|blah4}
#FUNCTION listcheck {%if( <list2 contains one or more items found in list1> , 1, 0)}

And if blah1 was removed from list2 and it contained only blah4, the function would evaluate to 0.

I've tried a few things in vain and I've found no functions that would allow me to do this inline or very efficiently. I know that I could do this with #LOOP or #FORALL, but I can't use those with #FUNCTION. Can anyone help me find a way to do this?
Reply with quote
jessew
Apprentice


Joined: 03 Mar 2003
Posts: 141

PostPosted: Mon Jun 14, 2004 5:13 pm   
 
You can use #LOOP #FORALL in functions by using %exec

#FUN listcheck {%if(%exec("#FORALL @list2 {#IF (%ismember(%i,@list1)) {1}}"),1,0)}


%exec returns a list of the results from all of the commands
it executed. So if it didn't find any members that your two lists shared the exec result list would be null and your function would return 0 etc.. So if you need to know how many it found you could change the %if to return the %numitems from the exec result


Cheers, Jesse
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Mon Jun 14, 2004 7:04 pm   
 
To generalize the function:
#FUN listcheck {%if( %exec( #FORALL %1 {#IF (%ismember( %i, %2)) {1}}), 1, 0)}

Syntax: @listcheck( list1, list2)
Returns 1 if two stringlists have any members in common. Otherwise, returns 0.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net