AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Compare nums (https://forums.alliedmods.net/showthread.php?t=61745)

Alka 10-08-2007 10:41

Compare nums
 
Hi.I want to compare the name length of players from server and print the lowest name...how can i make that ?!

Note:I know how to get the name length, but dunno how to compare the nums...

Thanks.

purple_pixie 10-08-2007 11:16

Re: Compare nums
 
Loop through them, comparing the strlen of the current one to the shortest found so far.

Save the shortest name and its length.

Do you also need to know which user it was?

( example to follow )
PHP Code:

#include <amxmodx>
#include <amxmisc>

show_shortest()
public 
names()
{
    new 
names[33][32],name_lengths[33],players[32],count,i,shortest_length,shortest_id,id;
    
get_players(players,count)
    for ( 
i=count ++ )
    {
        
id players[i]  // -1 so we can use a 32 cell array.
        
get_user_name(id,names[id],sizeof names[] - 1)
        
name_lengths[id] = strlen(names[id])
    }
    
shortest_length 999
    
for ( i=count ++ )
    {
        
id players[i] ;
        if ( 
name_lengths[id] < shortest_length )
        {
            
shortest_length name_lengths[id] ;
            
shortest_id id ;
        }
    }
    
log_amx("shortest name is %s, of %i characters (player %i )",names[shortest_id],shortest_length,shortest_id)


EDIT: Sorry, I hadn't tested it - should now work
(using id-1 is a total waste of time, [33] PWNS )

Alka 10-08-2007 11:32

Re: Compare nums
 
Humm...yeah i need to print shortest name...but i think i got it :D

Code:

Dumb code.
}

will this work? I think so... ?! O_o

purple_pixie 10-08-2007 11:34

Re: Compare nums
 
Possibly.

It's much harder to judge if something will work than write it :-D


Try it :-D

EDIT: And there's no point to strlen ShortestName every iteration - you can just save it.

No, it won't work.

If only because you got you > and < the wrong way around ...

That, and shortestname starts off empty. So its strlen is 0. So no other string can be shorter :-D


Re-EDIT: this:
PHP Code:

{
    new 
Players[32], NumPlayerShortestName[32],ShNameLen=9999;
    
get_players(PlayersNum);
 
    for(new 
Num i++)
    {
        
Player Players[i];
  
        static 
Name[32], NameLen ;
        
get_user_name(PlayerNamesizeof Name 1);
  
        
NameLen strlen(Name);
  
        if(
NameLen ShNameLen)
        {
            
copy(ShortestNamesizeof ShortestName 1Name);
            
ShNameLen NameLen
        
}
    }
    return 
ShortestName;


works fine.

I only changed a little - mainly swapping < for >, initialising shNameLen as 9999 and saving ShNameLen between iterations, rather than getting it each time.

Alka 10-08-2007 11:42

Re: Compare nums
 
Yeah lol, and i've puted the wrong sybol ">" , doh...i will try you'rs! :P

purple_pixie 10-08-2007 11:49

Re: Compare nums
 
My earlier code was slightly wrong.

For some unknown reason I thought I'd try using a 32 cell array, which threw everything to hell.

It's now right though - both codes I posted return or log the right name.

Alka 10-08-2007 12:12

Re: Compare nums
 
Yup, second one work fine and is optimized! Thanks :D


All times are GMT -4. The time now is 16:13.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.