Author Topic: Simple Script: Find Lowest Number  (Read 1001 times)

I need a script chunk that reads a series of vars and gives the one with the lowest number.

Example of script that makes the vars:

Quote
   for(%i=0;%i <= strlen(chathud.getText()); %i++) {
      %list = getsubstr(chathud.getText(),%i,%i+1);
      if(strstr(%string, %list) < -1)
         %lists[%i] = strstr(%string, %list);
   }
Obviously crappy non-real example, just to show that I need the lowest numbered strstr out of all the %list
  • 's

Code: [Select]
%best=1000;
%bestfound=-1;
for(%i=0;%i<%strings;%i++)
{
    if((%found=strstr(%string,%list[%i]))<%best)
    {
        %best=%found;
        %bestfound=%i;
    }
}
%result=%list[%bestfound]
Something like that?

Does that work? o.o I find leaving set vars to go by at first....a bit...limiting

Edit: That seems to use the %i as the lowest, I want the variable the %var means.

Example:
Code: [Select]
%num[%i] = 6;
%num[%i] = 9;
%num[%i] = 1;

I'd want it to find the lowest out of them, not the lowest %i out of them
« Last Edit: June 12, 2008, 05:59:12 PM by MegaScience »

Hmm, now that I've gone over it, VERY wrong.

I have ideas for doing it, but it'd involve too many repeating crap, I know there msut be a simple way.

It finds the index of the lowest, then sets %result to the value of it. It could easily do it the other way as well.

Anyway, I barely understood the example.

It could be done like

If you were comparing numbers, and just wanted the lowest number it would be something like
Code: [Select]
%best="none"
while((%check=%list[%i])!$="")
{
    if(%check<%best||%best$="none")%check=%best;
}

If you wanted to compare strings, well I just don't understand exactly what you are looking for, but...
Oh, and I missed that strstr returns -1 on failure, and that would beat the lowest.
Code: [Select]
%best=1000;
%bestfound=-1;
%i=0;
while(%list[%i]!$="")
{
    if((%found=strstr(%string,%list[%i]))<%best)
    {
        if(%found>-1)
        {
            %best=%found;
            %bestfound=%i;
        }
    }
}
%result=%list[%bestfound]

Nvm, I got it:
Code: [Select]
function findnamefromchat(%string) {
createnamelist();
for(%i=0;%i <= NPL_List.rowcount()-1; %i++) {
if(%i == 0)
%lowf = %i;
else if(%lowf < strstr(%string, $names[%i]))
%lowf = %i;
}
return $names[%lowf];
}
function createnamelist() {
for(%i=0;%i <= NPL_List.rowcount()-1; %i++)
$names[%i] = getField(NPL_List.getrowtext(%i),1);
}
That works, it finds the first occurrence of any name in a line and returns it.

This way, you can find who said the line clientside, Whatever :o