Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Compare arrays (https://forums.graalonline.com/forums/showthread.php?t=87682)

DustyPorViva 08-30-2009 09:05 AM

Compare arrays
 
Is there a simple way to compare two arrays like so:
PHP Code:

array1 = {1,1};
array2 = {2,2};
if (
array1 == array2

Without running loops? Ideally I'm looking for a single if check like my example, but my attempts have proven unsuccessful. Is there one of those 'secrets' or is it just not possible?

cbk1994 08-30-2009 09:14 AM

Quote:

Originally Posted by DustyPorViva (Post 1519709)
Is there a simple way to compare two arrays like so:
PHP Code:

array1 = {1,1};
array2 = {2,2};
if (
array1 == array2

Without running loops? Ideally I'm looking for a single if check like my example, but my attempts have proven unsuccessful. Is there one of those 'secrets' or is it just not possible?

I wonder if you could do something like

PHP Code:

if ((@ array1) == (@ array2)) {
  
// whatever


since @ converts arrays to strings. I know this works for some other stuff, such as "in" or "index".

Crow 08-30-2009 09:38 AM

Quote:

Originally Posted by cbk1994 (Post 1519710)
I wonder if you could do something like

PHP Code:

if ((@ array1) == (@ array2)) {
  
// whatever


since @ converts arrays to strings. I know this works for some other stuff, such as "in" or "index".

That works, yes, but the problem is that the order of the array elements has to be the same as well. Could sort both of them before checking if they are equal though :)

WhiteDragon 08-30-2009 04:29 PM

Quote:

Originally Posted by DustyPorViva (Post 1519709)
Is there a simple way to compare two arrays like so:
PHP Code:

array1 = {1,1};
array2 = {2,2};
if (
array1 == array2

Without running loops? Ideally I'm looking for a single if check like my example, but my attempts have proven unsuccessful. Is there one of those 'secrets' or is it just not possible?

No, it's not possible. If you want to check every item in the array you'll need to loop through it, although you can break the loop once it returns false.

The conversion to a string with @ does a loop internally to concatenate all the items, which would probably be faster since it's native, however, it could get slower as your array gets larger because in your implementation it'll stop looping once you hit a difference.

If you just want to check if all the items are the same, rather than in the same order, I would do what Crow said and run a sort on them (probably TGraalVar.sortascending() would be the fastest). If you have a HUGE array, then you could also use the introsort I wrote awhile back and it would probably perform better than sortascending().

DustyPorViva 08-31-2009 04:20 PM

Well I was only interested in comparing small arrays of tiles in the exact order anyways, so that's exactly what I wanted. Thanks!


All times are GMT +2. The time now is 11:17 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.