Jump to content
JustRoger

Compare var string to an array

Recommended Posts

I searched for a long while for this before posting.   (fairly new to this so keep the snark to a min)

I am trying to compare a string against an array:

 

_Wep = "Weapon1;

_ArrayWeps = ["Weapon1", "Weapon2", "Weapon3"];

_IsInArray = _Wep in _ArrayWeps;  // is _Wep inside the array

 

should give me a boolean for _isInArray of true/false? or do I have to do:

if (_Wep in _ArrayWeps) then { // true};

 

OR if there is a better way to please do share.  I do appreciate the input.

Share this post


Link to post
Share on other sites

At first I recommend using poseidon to edit scripts for arma 3.

It's a true game changer and in ongoing development.

Helps spotting syntax errors and takes you to the wiki when hitting F1 with a script command selected by the cursor.

 

To your problem, you can use "find" to either return the array index or a bool variable, like this:

 

_weapon = "Nope";
_ArrayWeps = ["Weapon1", "Weapon2", "Weapon3"]; 

_output = _ArrayWeps find _weapon; //_output is -1, because "Nope" is not in the array

_weapon = "Weapon2";
_ArrayWeps = ["Weapon1", "Weapon2", "Weapon3"]; 

_output = _ArrayWeps find _weapon; //_output is 1, because "Weapon2" is the second element (first array index is 0)


//or if you simply want to check if the weapon is inside the array:

_weapon = "Nope";
_ArrayWeps = ["Weapon1", "Weapon2", "Weapon3"]; 

_output = _ArrayWeps find _weapon >= 0; //false

_weapon = "Weapon2";
_ArrayWeps = ["Weapon1", "Weapon2", "Weapon3"]; 

_output = _ArrayWeps find _weapon >= 0; //true

 

Cheers

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, justroger said:

should give me a boolean for _isInArray of true/false?

Yes you can use in, and your _isInArray would be true/false. Barring any mistakes (missing closing " on _wep) your current code would return false as you have misspelled Weapoon1 (maybe on purpose?) and remember in is case sensitive ("weapon1" would be false).

  • Like 1

Share this post


Link to post
Share on other sites
3 minutes ago, Larrow said:

Yes you can use in, and your _isInArray would be true/false. Barring any mistakes (missing closing " on _wep) your current code would return false as you have misspelled Weapoon1 (maybe on purpose?) and remember in is case sensitive ("weapon1" would be false).

 

I used grumpy's version and sorted it by:

 

if (_Output >=0) then { code...};   // true - since 0 is first weapon, 1 is the second, and so on.

 

I just needed a comparison from string value against an array that I setup.  I ran across Killzone_Kid's info on making everything uppercase then doing a comparison.  Since I am copying the Class_names directly over I don't think case sensitive will be an issue. (hopefully).

Share this post


Link to post
Share on other sites
14 hours ago, justroger said:

 

I used grumpy's version and sorted it by:

 

if (_Output >=0) then { code...};   // true - since 0 is first weapon, 1 is the second, and so on.

 

I just needed a comparison from string value against an array that I setup.  I ran across Killzone_Kid's info on making everything uppercase then doing a comparison.  Since I am copying the Class_names directly over I don't think case sensitive will be an issue. (hopefully).

As @Larrow said, you could also use "in" to return bool, should be faster.

If you retrieve weapon class names from script or via copy paste then case sensitivity should not be an issue.

 

Cheers

Share this post


Link to post
Share on other sites

Just a question, not saying yours is not pertinent: What do you intend to do with that? Checking if a weapon class is worthy, testing addon presence?

 

Share this post


Link to post
Share on other sites
On 9/18/2017 at 11:10 PM, pierremgi said:

Just a question, not saying yours is not pertinent: What do you intend to do with that? Checking if a weapon class is worthy, testing addon presence?

 

 

I was building a script to compare one thing against a pre-set array.   

I just used weapons as an example because it is mentally easier to sort (at least for me).  

 

Just testing event handlers, pulling out information from their arrays comparing it against my preset array, all of it is just testing, learning the logic and trying to stream-line the code. 

example:  If player fires 6.5mm ammo type,  is that ammo type in my array list, if yes then execute code.

 

I'm sorting specific ammo classes then once that specific  ammo class is matched with the ammo class in the array I have this for notification:  hint format ["ammo type found: %1", _ammoType];

 

player addEventHandler ["fired", {_this execVM "checkType.sqf"}]  // my init.sqf

 

_ammoType = _this select 4;

 

_ArrayAmmo = ["B_65x39_Caseless", "B_65x39_Caseless_green"];  // short example of the array - MX/C/M/SW/3GL and Katiba, Type 115

 

if  (_AmmoType in _ArrayAmmo) then                     // used this from the above suggestion from @Larrow

{

         playsound "beep";                                           // One beep for found. (defined in description.ext)

                                                                                

Else {     

        hint format ["ammo type: %1", _ammoType];  // show me what class name was fired but not in the array.

        playSound "Beep-Beep"';                                // Two beeps to notify not found. (defined in description.ext)

        };

};

 

This logic will be used in a more complex script I am working on.  I appreciate everyone's input on helping me this far.

seriously thank you

 

 

 

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×