Comparing array values

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
Faceless_Soldier
New User
Posts: 15
Joined: Mon Jun 30, 2008 9:07 pm

Comparing array values

Post by Faceless_Soldier »

Hi,

Can someone point me in the direction of how to properly code the following situation. Preferably in the "right" or "best" practice.
More or less, I want to compare each value from 2 separate arrays, if the value is present in both, do this, if not do that. I have been looking at methods to do this so and have usually ended up with a nested foreach. I haven't found a lot in the scripting tutorials regarding "if in array".

Context: It is for a command script that powers up certain skill-set. Eg. powering up all warrior type skills. At the same time I want it to remove any skills that are not within the array of warrior skills. I could just do a blanket 'set all skills to 0' but that doesn't seem very professional or right to me to have a players skills set to 0 then to 100 straight after. It should just set it to 100, while removing irrelevant skills. The less blue spam on the screen the better.

Any opinions, direction or advice?
Thanks.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Comparing array values

Post by Austin »

You can do if ( variable in array )

Code: Select all

  var array_a = array(1, 2, 3, 4, 5, 6, 7);
  var array_b = array(6, 7, 8, 9, 10, 11);

   foreach value in ( array_a )
      if ( value in array_b )
           .... do what ever
      endif
   endforeach
If you use that approach, you just wont know where it is in array_b - just that its there.
Faceless_Soldier
New User
Posts: 15
Joined: Mon Jun 30, 2008 9:07 pm

Re: Comparing array values

Post by Faceless_Soldier »

Thanks a lot! :D

I figured there would be something like that but I couldn't find it here or through search or the documentation. Hard to find something when you don't know what your looking for. That worked a treat. My other solutions were less then impressive.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Comparing array values

Post by Austin »

Better yet.. Turley just put this into the core's code
Log Message:
-----------
Changed: "in" keyword now returns the arrayindex or 0 if no match was found
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Comparing array values

Post by Turley »

your posting reminds me :P i think i wanted to change it since i started with the core :)
Post Reply