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
Darkmere
Novice


Joined: 23 May 2003
Posts: 31

PostPosted: Thu Dec 11, 2003 9:07 pm   

Maximum value function
 
What I am trying to do, is search a list of variables (that contain number values) and trying to get the max value, as well as the name of the variable.

I know there must be a simple way to do this, but here is what I have so far:

Code:

#VAR PlayerScoreNames {Zuel|Gorfang|Ezikiel|Danlo|Arianna|Nez|Scarlet|Ardent|Rathan|Kane|Cagney|Bartholomew|Zangief|Vinnie|Aelric|Typhoeus|Gar|Malaclypse|Kimora|Catalina|Grevin|Cumur|Jeff|Karna|Ailegore|Bishop|Mudician|Cailean}

#VAR PlayerScores {@ZuelTotal|@GorfangTotal|@EzikielTotal|@DanloTotal|@AriannaTotal|@NezTotal|@ScarletTotal|@ArdentTotal|@RathanTotal|@KaneTotal|@CagneyTotal|@BartholomewTotal|@ZangiefTotal|@VinnieTotal|@AelricTotal|@TyphoeusTotal|@GarTotal|@MalaclypseTotal|@KimoraTotal|@CatalinaTotal|@GrevinTotal|@CumurTotal|@JeffTotal|@KarnaTotal|@AilegoreTotal|@BishopTotal|@MudicianTotal|@CaileanTotal}
 


When I type:
#SHOW %max(%expandlist(@PlayerScores,", "))
I get the result '13309' (This is correct)

Now I want the variable name, so I made the list @PlayerScoreNames with the same order as the @PlayerScores

When I type:
#SHOW %pos(%max(%expandlist(@PlayerScores,", ")),@PlayerScores)
I get the result '18' (This is not correct) It should be '5'

Therefore when I put:
#SHOW %item(@PlayerScoreNames, %pos(%max(%expandlist(@PlayerScores, ", ")), @PlayerScores))
I get the result 'Malaclypse' (should be Arianna)


Thanks in advance,
-Darkmere
Reply with quote
Toetag
Magician


Joined: 10 Oct 2000
Posts: 356
Location: USA

PostPosted: Thu Dec 11, 2003 9:41 pm   
 
I don't have a fix, i've been thinking about this the last couple of nights, it would be nice to have a %itemindex("John", @playerlist) function, that would return the item index for John.

I believe the problem is that you are using a string function against a string list.

I used the following in my testing:

#VAR PlayerScoreNames {Zuel|Gorfang|Ezikiel|Danlo|Arianna|Nez|Scarlet|Ardent|Rathan|Kane|Cagney|Bartholomew|Zangief|Vinnie|Aelric|Typhoeus|Gar|Malaclypse|Kimora|Catalina|Grevin|Cumur|Jeff|Karna|Ailegore|Bishop|Mudician|Cailean}


#VAR PlayerScores {12000|12000|12000|12000|13000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000|12000}

I used your commands that you showed:

#SHOW %max(%expandlist(@PlayerScores,", ")) returns: 1300
#SHOW %pos(%max(%expandlist(@PlayerScores,", ")),@PlayerScores) Returns: 25

25 characters into the string list would be: (highlighted in red)
12000|12000|12000|12000|13000

it's giving you character position, not item index.

I've been trying to write a routine to come up with an index alias but i'm still working on it.
Reply with quote
Darkmere
Novice


Joined: 23 May 2003
Posts: 31

PostPosted: Thu Dec 11, 2003 9:52 pm   
 
I see, I feared this was the case when I started looking closer at the short help file.

If you, or anyone else, has an alternate idea to solve this problem please post!
If you are really bored or excited, you can try to solve the entire problem: Find the top 3 values of variables in the list

Thanks,
-Darkmere
Reply with quote
Darkmere
Novice


Joined: 23 May 2003
Posts: 31

PostPosted: Fri Dec 12, 2003 1:23 am   
 
A good friend of mine showed me %item, rather then %pos

Code:
#SHOW %item(@PlayerScoreNames, %ismember(%max(%expandlist(@PlayerScores, ", ")), @PlayerScores))

Outputs: Arianna

-Darkmere
Reply with quote
Toetag
Magician


Joined: 10 Oct 2000
Posts: 356
Location: USA

PostPosted: Fri Dec 12, 2003 2:34 am   
 
neat trick. I didn't realize you could use the %ismember function in that manner and I've been using it for years.

Great Call. I can stop my poor attempt and creating my own little function.

And I thought only Lightbulb taught me new tricks. Smile
Reply with quote
Danlo
Magician


Joined: 28 Nov 2003
Posts: 313
Location: Australia

PostPosted: Fri Dec 12, 2003 11:27 am   
 
I played with this for a few hours, and came up with the following.
Whenever you add to a player's score, call upon this function, parsing the user's name to it. It will update the name-list from order of Highest score to Lowest score:

#var PlayerScoresTwo {%expandlist( @PlayerScores, "|")}
#var PlayerScoreNamesTwo {@PlayerScoreNames}
#var CurrentPlayerScore {@%1Total}
#var CurrentScorePosition {%ismember( %1, @PlayerScoreNamesTwo)}
#IF (@%1Total>{%item( @PlayerScoresTwo, 1)}) {#delnitem PlayerScoreNames (@CurrentScorePosition)
#var PlayerScoreNames {%insert( %1~|, @PlayerScoreNames, 1)}} {#loop 1,%eval(%ismember( @CurrentPlayerScore, @PlayerScoresTwo)-1) {#IF ({@CurrentPlayerScore}>{%item( @PlayerScoresTwo, %{i})}) {#var NewScorePosition {%eval( %{i})}} {}}
#delnitem PlayerScoreNames (@CurrentScorePosition)
#var PlayerScoreNames {%insert( ~|%1, @PlayerScoreNames, %pos( %concat( ~|, %item( @PlayerScoreNamesTwo, @NewScorePosition)), @playerscorenames))}}
#var PlayerScores {%null}
#FORALL @PlayerScoreNames {#additem PlayerScores {%concat( "@", %i, "Total")}}

Danlo
Reply with quote
Darkmere
Novice


Joined: 23 May 2003
Posts: 31

PostPosted: Tue Dec 16, 2003 1:59 am   
 
Ok, update and new question:

Here is the working version we came up with that does exactly as it should.

The questions is, why this is buggy with variables with values over 10000.
Example: If the top (highest) variable is over 10000 and you do the alias for a 5000 score variable, it bumps it up to first place. If you set the highest to a score of 9950 and try again, it works perfect. I have been told that this code works in an earlier version, though I am on 6.66 beta. Could this be a bug in zMUD, or is there a fix for the code?



Code:
#var PlayerScoresTwo {@playerscores}
#var PlayerScoreNamesTwo {@PlayerScoreNames}
#var CurrentPlayerScore {@%1Total}
#var CurrentScorePosition {%ismember( %1, @PlayerScoreNamesTwo)}
#IF (@currentscoreposition=1) {} {
  #var LoopCount {%eval( @currentscoreposition+1)}
  #IF (@CurrentPlayerScore<%item( @PlayerScoresTwo, %eval( @CurrentScorePosition-1))) {} {
    #IF (@%1Total>%item( @PlayerScoresTwo, 1)) {
      #delnitem PlayerScoreNames (@CurrentScorePosition)
      #var PlayerScoreNames {%insert( %1~|, @PlayerScoreNames, 1)}
      } {
      #loop 1,%ismember( @CurrentPlayerScore, @PlayerScoresTwo) {
        #add loopcount {-1}
        #IF (@CurrentPlayerScore>%item( @PlayerScoresTwo, @loopcount)) {#var NewScorePosition {%eval( @loopcount)}} {}
        }
      #delnitem PlayerScoreNames (@CurrentScorePosition)
      #var PlayerScoreNames {%insert( ~|%1, @PlayerScoreNames, %pos( %concat( ~|, %item( @PlayerScoreNamesTwo, @NewScorePosition)), @playerscorenames))}
      }
    #var PlayerScores {%null}
    #FORALL @PlayerScoreNames {#additem PlayerScores {%concat( "@", %i, "Total")}}
    }
  }



-Darkmere
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Wed Dec 17, 2003 4:39 pm   
 
Use %number() to ensure that you are comparing numbers, not strings.
#IF (%number( @CurrentPlayerScore) < %number( %item( @PlayerScoresTwo, %eval( @CurrentScorePosition-1))))
Reply with quote
Darkmere
Novice


Joined: 23 May 2003
Posts: 31

PostPosted: Wed Dec 17, 2003 5:39 pm   
 
Thanks LightBulb,

That was the trick, it must have been comparing string values when it was over 10,000.
%number fixed that problem.

Thanks,
-Darkmere
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