Raised This Month: $ Target: $400
 0% 

Compare nums


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-08-2007 , 10:41   Compare nums
Reply With Quote #1

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.
__________________
Still...lovin' . Connor noob! Hello

Last edited by Alka; 10-08-2007 at 11:11.
Alka is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 10-08-2007 , 11:16   Re: Compare nums
Reply With Quote #2

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 )

Last edited by purple_pixie; 10-08-2007 at 11:50.
purple_pixie is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-08-2007 , 11:32   Re: Compare nums
Reply With Quote #3

Humm...yeah i need to print shortest name...but i think i got it

Code:
Dumb code.
}
will this work? I think so... ?! O_o
__________________
Still...lovin' . Connor noob! Hello

Last edited by Alka; 10-08-2007 at 11:42.
Alka is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 10-08-2007 , 11:34   Re: Compare nums
Reply With Quote #4

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.

Last edited by purple_pixie; 10-08-2007 at 11:45.
purple_pixie is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-08-2007 , 11:42   Re: Compare nums
Reply With Quote #5

Yeah lol, and i've puted the wrong sybol ">" , doh...i will try you'rs!
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
purple_pixie
Veteran Member
Join Date: Jun 2007
Location: Winchester, England
Old 10-08-2007 , 11:49   Re: Compare nums
Reply With Quote #6

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.
purple_pixie is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 10-08-2007 , 12:12   Re: Compare nums
Reply With Quote #7

Yup, second one work fine and is optimized! Thanks
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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