Aller au contenu

Photo

GetIntArrayIndex() fancies


  • Veuillez vous connecter pour répondre
6 réponses à ce sujet

#1
Phaenan

Phaenan
  • Members
  • 315 messages
I just lost twenty minutes running in circles because of that prankster of a function, so I thought I might drop a word of caution here.
If you're assuming GetIntArrayIndex() returns a 0 based index like every other functions and structure in there, think again. (pastie)

int[] aiphaetest;
aiphaetest[0] = 541;
aiphaetest[1] = 582;
aiphaetest[2] = 742;
aiphaetest[3] = 562;
aiphaetest[4] = 589; 
Phae_print(IntToString(GetIntArrayIndex(aiphaetest, 562))); return;[/code]

This code displays "4" , meaning a match at [0] will be returned as index 1, etc.



That does make sense when looking at the function source in core_h, however : (pastie)

// return the index of the first occurence of an integer in an integer array
int GetIntArrayIndex (int[] arArray, int nItem) {
    int nSize = GetArraySize(arArray);
    int bFound;

    int i;
    for (i = 0; i < nSize && !bFound; i++) {
    
        bFound = (arArray[i] == nItem);
    }

    return (bFound ? i : -1) ;
}



Put like that, the for statement will increment the variable i before checking if the conditional is false. That's plainly what for does for a living. Duh. :P



The same concept error is found in GetObjectArrayIndex() so that one will behave the same.



// Edit :
Added some pasties, code coloring here would be nice. Nii~iice. I tell you. :o

Modifié par Phaenan, 27 novembre 2009 - 12:45 .


#2
AND04

AND04
  • Members
  • 154 messages
thx, didn't use it yet but will defnitly save me some trouble in the future :)

Modifié par AND04, 27 novembre 2009 - 02:38 .


#3
Nodrak

Nodrak
  • Members
  • 144 messages
Thanks for the heads up, I tend to check the function's definition before I use it, but sometimes I don't look very close.



Also AND04, your sig appears to be bugged (or its my IE8 >.<).

#4
Sarcen

Sarcen
  • Members
  • 38 messages
I find that a really weird way of writing a function like that, a little bit redundant too (the bFound checking each loop), i would create that function like, and this will ofc return a 0 based index.



int GetIntArrayIndex (int[] arArray, int nItem) {

int nSize = GetArraySize(arArray);

int i;

for (i = 0; i < nSize; i++) {

if (arArray == nItem)

return i;

}



return -1;

}

#5
AND04

AND04
  • Members
  • 154 messages

Nodrak wrote...

Thanks for the heads up, I tend to check the function's definition before I use it, but sometimes I don't look very close.

Also AND04, your sig appears to be bugged (or its my IE8 >.<).


idd :/ fixed - thx

#6
Nodrak

Nodrak
  • Members
  • 144 messages
The distinction appears to be intentional so that the size as per GetArraySize matches with the GetArrayIndex. In code, it is obvious the array indicies are 0-9 in an array that would return size 10. This function thus returns 1-10 for a size 10 array and -1 for notfound. (I thought it returned 0 for an empty array too for a sec)

Modifié par Nodrak, 27 novembre 2009 - 03:52 .


#7
Phaenan

Phaenan
  • Members
  • 315 messages
Yeah, it's probably intentional, come to think of it, otherwise there would be some issues here and there when the OC scripts use those functions. Still, one line of commentary isn't a bad idea when one's writting some quite unorthodox code. ^_^

Anyway, rewrote my own, which happens to be exactly the same as Sarcenn's since... well, that's the most common way of doing it, I'd say. :o