Jump to content
Sign in to follow this  
eagledude4

Question about strings and arrays

Recommended Posts

I need some help with the below code:

_classname = _this;
_count = ???
_Var = format ["%1_%2", _classname, _count];
ServerVcls = ServerVcls + [_Var];

Result:

ServerVcls = ["Atv_1", "Truck", "Atv_2", "Car"];

I think I can get the number by counting the number of times "Atv" appears in the array, but I don't know of any functions to do this.

something like this: http://stackoverflow.com/questions/15506521/actionscript-3-count-number-of-times-a-string-appears-in-an-array

Edited by eagledude4

Share this post


Link to post
Share on other sites

So what you want to do is find a string within a string. There is an A3 BIS function for this called BIS_fnc_inString but that does not help you in A2 unless you re-create it. There is a CBA function (for A2 and A3) called CBA_find that will also find a string within a string. If you do not want to use CBA you could create your own "find a string in a string" code. Since you know exactly what you find, here is a simple way to do that. It is a little messy but it is quick and should work (but is untested).

// Set Scope
private ["_unicode", "_vehicleCount", "_string", "_array"];

// Init ServerVcls (unless this is done somewhere else).
ServerVcls = ["Atv_1", "Atv_2", "Atv_3"];

// Init priave variables needed later.
_unicode = [41,74,76]; // A, t, v in Unicode (case-sensitive)
_vehicleCount = 0; // A count of the times an instance of "Atv" is found in ServerVcls

// Loop through all of the elements of ServerVcls using forEach.
{
   _string = _x; // Get the (next) string value of the selected element in the array ServerVcls.
   _array  = toArray _string; // Convert the String into an array.

   //If the array is more than three elements then it could start with "Atv".
   if (count _array > 3) then
   {
       // If the first three elements of _array equal the first three elements of _unicode then the string begins with "Atv" and increment the vehicle count.
       If ((_array select 0 == _unicode select 0) && (_array select 1 == _unicode select 1) && (_array select 2 == _unicode select 2)) then 
       {
              _vehicleCount = _vehicleCount + 1;
       };
   };
}; forEach ServerVcls;

You can make this cleaner by extracting the first three elements in _array and then converting it back to a string using toString then just compare strings instead of array elements.

Share this post


Link to post
Share on other sites

I can't use fixed variables, so the whole unicode thing is out of the question.

I was thinking of something like this:

_count = INV_ServerVclArray find _classname;

if (_classname in INV_ServerVclArray) then {
_var = format ["%1_%2", _classname, _count];
} else {
_var = format ["%1", _classname];
//hint format ["%1", _var];
};

hint format ["%1", _var];

hints "any", but

_count = INV_ServerVclArray find _classname;

if (_classname in INV_ServerVclArray) then {
_var = format ["%1_%2", _classname, _count];
} else {
_var = format ["%1", _classname];
hint format ["%1", _var];
};

//hint format ["%1", _var];

hints the classname as it should.

Why is _var nil outside of the conditions?

Edited by eagledude4

Share this post


Link to post
Share on other sites

the find command returns the index of the first element it encounters in the array, not all of the elements. So, assuming there is a match, count will only ever be 1.

So, when I read your first post I assumed you were looking for names of vehicles, not actual class names. If you are looking to simply count the number of a certain type of vehicle, that is a differrent process. What exactly do you want to know?

Share this post


Link to post
Share on other sites

I'm looking for classnames because I want to work with strings. By looking at the http://community.bistudio.com/wiki/find page, i don't think that can be true about the elements if it was able to find the second element, right?

the ServerVcls starts as an empty array, then gradually add the elements. It's not a static variable.

I also found this in the count page of the wiki:

This one, on the other hand, where all elements are strings, just like the tested element, will return the correct result of 1:

_arr=["one","two","three"];{_x=="one"} count _arr

so I think I can use this as an alternative to find, assuming it doesn't have the same flaw that you suggested find did. I just hope these functions dont look for whole words.

I edited this post as well as post #3 pretty extensively so make sure you give it another read before replying. Also, conveniently, this is for an arma3 script. I just posted here because I assumed the necessary code was going to be the same for arma2 as it would for arma3, and I'd get more views by posting it here instead. I'm hoping that's okay, I don't see why it wouldn't be.

Edited by eagledude4

Share this post


Link to post
Share on other sites
I can't use fixed variables, so the whole unicode thing is out of the question.

I was thinking of something like this:

_count = INV_ServerVclArray find _classname;

if (_classname in INV_ServerVclArray) then {
_var = format ["%1_%2", _classname, _count];
} else {
_var = format ["%1", _classname];
//hint format ["%1", _var];
};

hint format ["%1", _var];

hints "ANY", but

_count = INV_ServerVclArray find _classname;

if (_classname in INV_ServerVclArray) then {
_var = format ["%1_%2", _classname, _count];
} else {
_var = format ["%1", _classname];
hint format ["%1", _var];
};

//hint format ["%1", _var];

hints the classname as it should.

Why is _var nil outside of the conditions?

Because you didn't declare _var outside of the scope of your if/then statement in the first example, so it is only seen within the scope of your if/then statement.

Also, you say you want to work with classnames, but none of your examples show classnames??? Is this just for simplicity? Looks like you're only using the vehicleVarName.

_classname = typeOf _this;

How is code being passed to this script? Is this for creating vehicles? If not, why not do this when they are created?

If you are only using certain vehicles, I would use a switch statement, then you can number each type correctly.(untested code below, but I think you can get the idea)

private ["_classname", "_name", "_Var"];

_classname = typeOf _this;
_name = vehicleVarName _this; 

switch (_classname) do
{
   case "ATV_US_EP1":
   {
       private ["_count"];
       _count = 1;
       _Var = format ["%1_%2", _name, _count];
       ServerVcls = ServerVcls + [_Var];
       _count = _count +1;
   };

   case "MTVR":
   {
       private ["_count"];
       _count = 1;
       _Var = format ["%1_%2", _name, _count];
       ServerVcls = ServerVcls + [_Var];
       _count = _count +1;
   };

   ...
};

What exactly are you using this for? You may be better off posting in the A3 forum, since you stated this was for A3, as many people have jumped ship...

Edited by panther42

Share this post


Link to post
Share on other sites

Yes, the code is for creating vehicles. I also fixed the scope issue, thank you.

So far this is what I've come up with, and it seems to work.

_classname = _this select 0;
_logic	   = _this select 1;

_Vcl = _classname createVehicle getpos _logic;
_Count = {_x == _classname} count ServerVcls_Count;

_Var = format ["%1_%2", _classname, _Count + 1];

_Vcl setVehicleVarName _Var;
_Vcl setVariable ["Owner", name player, true];
_Vcl setVariable ["Class", _classname, true];
_Vcl setdir getdir _logic; 

clearWeaponCargo _Vcl; 
clearMagazineCargo _Vcl;

_Vcl lock true;

INV_VehicleArray = INV_VehicleArray + [_Vcl];
ServerVcls = ServerVcls + [_Var];
publicVariable "ServerVcls";
ServerVcls_Count = ServerVcls_Count + [_classname];
publicVariable "ServerVcls_Count";

Thanks all for the help.

Edited by eagledude4

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
Sign in to follow this  

×