Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-30-2009, 09:05 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
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?
Reply With Quote
  #2  
Old 08-30-2009, 09:14 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by DustyPorViva View Post
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".
__________________
Reply With Quote
  #3  
Old 08-30-2009, 09:38 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by cbk1994 View Post
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
__________________
Reply With Quote
  #4  
Old 08-30-2009, 04:29 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by DustyPorViva View Post
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().
Reply With Quote
  #5  
Old 08-31-2009, 04:20 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Well I was only interested in comparing small arrays of tiles in the exact order anyways, so that's exactly what I wanted. Thanks!
Reply With Quote
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 +2. The time now is 12:41 PM.


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