TinTin++ Mud Client The TinTin++ message board

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
TinTin++ Mud Client

How to perform an action on all like variables

 
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> General Discussion
View previous topic :: View next topic  
Author Message
steveb



Joined: 10 Oct 2005
Posts: 11

PostPosted: Mon Oct 31, 2005 10:37 am    Post subject: How to perform an action on all like variables Reply with quote

I could swear an example similar to this was posted earlier, but here goes..

Any ideas on how to perform an action on all similar named variables? For example, if there are several lists list1, list2, list3,... and you wanted a total length of all lists. I know you could write something to overcompensate the lists, like...
#loop {1 100}{
#list {list&0}{len}{tmp};
#math {ListTotal}{$ListTotal + $tmp}
}
...knowing that for all listx that do not exist, the len will return 0. Is there a way to do this that doesn't rely on guessing the variable name?

Steve
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3770

PostPosted: Mon Oct 31, 2005 12:10 pm    Post subject: Reply with quote

You can use #forall in combination with a variable holding all the list names I think.
Back to top
View user's profile Send private message Send e-mail
steveb



Joined: 10 Oct 2005
Posts: 11

PostPosted: Mon Oct 31, 2005 4:57 pm    Post subject: Reply with quote

Had thought of trying that too, but that's even more static than the loop solution. You'd need to declare each list in the #forall

Another solution I had involved recursive commands, something like

#alias count {
#list {list$listnum}{len}{temp};
#if {$listnum > 0}{
#math {listnum}{$listnum + 1};
#math {totallen}{$totallen + $temp};
count
}
}

Or some such nonsense. I'm leary of having commands call themselves. It's always good to avoid potential infinite loops. This one seems fairly safe.

Steve
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3770

PostPosted: Mon Oct 31, 2005 6:26 pm    Post subject: Reply with quote

Code:

#alias count {
     #list {list$listnum}{len}{temp};
     #if {$listnum > 0}{
          #math {listnum}{$listnum + 1};
          #math {totallen}{$totallen + $temp};
          #delay 0 count
     }
}

You'll need the delay when going beyond 30 loops I think.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> General Discussion All times are GMT - 5 Hours
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
Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads
TinTin++ Homepage

Powered by phpBB © 2001, 2002 phpBB Group