Jump to content
Sign in to follow this  
riouken

For-Loop inside of a forEach loop

Recommended Posts

I am trying to convert a multi-dimensional array to a normal array dynamically( I dont know how big each array will be)

Ie. Turn this:

[["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]]

Into this:

["30Rnd_556x45_Stanag","30Rnd_556x45_Stanag","SmokeShell","HandGrenade_West"]

I tried doing it this way:

_MagazinesRuckList = [["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]]

_tempRuckMagList = [];

{
   _tempnum = _x select 1;
   _tempclass = _x select 0;
   for [{_i=0}, {_i<_tempnum}, {_i=_i+1}] do
   {
       _tempRuckMagList + [_tempclass];
   };
} forEach _MagazinesRuckList;

It does not return an error in the rpt, but it just returns an empty array.

Anyone have any ideas? Thanks in advance.

Share this post


Link to post
Share on other sites

_tempRuckMagList + [_tempclass];

this should be changed to

_tempRuckMagList = _tempRuckMagList + [_tempclass];

Share this post


Link to post
Share on other sites

Thanks man, I have been looking at this so long i missed that error.

Share this post


Link to post
Share on other sites

take alook at my preset loadouts by player UID in my sig spoiler, youll see many examples of what you want to acomplish i think.

Share this post


Link to post
Share on other sites
take alook at my preset loadouts by player UID in my sig spoiler, youll see many examples of what you want to acomplish i think.

Thanks the project I am working on was inspired by your loadout script. I am working on an addon version that will off load all the work from the mission maker to the end user. But still let the mission maker keep control over what users can have.

I have most everything working now.

I am just trying to make the enduser experience as easy as possible. I have the above problem solved. I am just still having a problem with this:

http://forums.bistudio.com/showthread.php?t=118498

Its chopping off the "" from my stringed array and i need those there for the weapon classnames.

Share this post


Link to post
Share on other sites

Ok, im curious as to what your end goal differs from mine, as in mine the user can ofcourse edit the files itself and send to mission maker, but im asuming you are aiming at some kind of clientside config / addon or something.

text command takes off the "" from any string

format command applies "" to any text.

Share this post


Link to post
Share on other sites

We have close to 100 people in our unit and 65+ plus show up for our unit events. Our command staff and Mission team has enough work with just planning and making the missions, and mission conditions can change, resulting in gear changes.

That becomes a real challenge for our mission team to keep a list like that up to date. And that does not count for last minute changes to gear if there was a mistake, etc...

You are right my addon uses a userconfig for the user loadouts. That way instead of sending the array to the mission maker they just copy it into their userconfig. I am also allowing three loadouts to select from.

So far I have the loadouts working great, Im just working on how the user will get the arrays to copy into their userconfig.

as to the strings:

I am using this:

thearray = ["mygun1","mygun2","mygun3"];

_string = "";

for "_i" from 0 to (count thearray) -1 do
{
   if (_i == ((count thearray) -1)) then 
   {
       _string1 = format ["%1", thearray select _i];
       _string = _string + _string1;
   }else 
   {
       _string1 = format ["%1,", thearray select _i];
       _string = _string + _string1;
   };
};

copyToClipboard _string;

I need it to out put this:

"mygun1","mygun2","mygun3"

but its giving me this:

mygun1,mygun2,mygun3

I tried using ( "'%1'" ) single quotes but the game will not accept that when i import it back in. It has to be wrapped in "";

Edited by Riouken

Share this post


Link to post
Share on other sites
_tempRuckMagList + [_tempclass];

this should be changed to

_tempRuckMagList = _tempRuckMagList + [_tempclass];

Or even better:

_tempRuckMagList set [count _tempRuckMagList, _tempclass];

Much more efficient with little effort :)

Share this post


Link to post
Share on other sites

Thanks but, Sorry Im not quite following why thats more efficient. It counts the array each time.

Share this post


Link to post
Share on other sites

Yes, but it modifies the original array instead of creating a new one every time (which is what the + operator does).

Share this post


Link to post
Share on other sites

good idea Riouken with the client side config, wish i had the skills and thought of it first :)

why do you need out of array classnames? this seems inefficient, as an array can employ for loops or foreach loops, much easier.

Share this post


Link to post
Share on other sites

Yea Demonized I use if statments and forEach a ton in my loadout script, thats pretty much how im handling everything.

This is how I have to have my userconfig setup:

RSLO_lo2_weapons_user[] = {"ACE_M4A1_ACOG","ACE_BackPack_ACR_FL"};

this will import into the game as a normal array:

["ACE_M4A1_ACOG","ACE_BackPack_ACR_FL"]

normaly when you pull your list of weapons you get an array like this:

["ACE_M4A1_ACOG","ACE_BackPack_ACR_FL"]

But in my config It cant have the [] around it. So im trying to format that all out so that when its copied to the clipboard the user can just directly paste it into the userconfig. with out haveing to delete the []. Lol I have spent several hours(and i havent solved it yet) on trying to save them 2 seconds.

Edited by Riouken

Share this post


Link to post
Share on other sites

Sweet Deadfast, I didn't think of trying triple quotes, lol I used "","""","''" but not """"""

Let me go test this out

Thanks

Share this post


Link to post
Share on other sites

Edit: solution found at bottom of post.

i think the problem with making a single string like:

weapon1,launcher1,backpack

into:

"weapon1","launcher1","backpack"

will not work, as it will probably end up at best being:

""weapon1","launcher1","backpack"" resulting in enduser having to remove first and last "".

you could try:

_string = format ["'%1','%2,'%3'",weapon1,launcher1,backpack];
_without_qoutes = text _string;

or maybe it will work with:

_string = text format ["'%1','%2,'%3'",weapon1,launcher1,backpack];

Edit: then again, if its dynamic addition of x amount weapons this approach may probably not work.

Maybe you can setup lines for different amount of types:

if (count (weapons _unit) == 1) then {

if (count (weapons _unit) == 2) then {

if (count (weapons _unit) == 3) then {

if (count (weapons _unit) == 4) then {

etc...

Anyhow, if you are at a loss, and can make use of my scripts, im ok with you using the getMyPresets.sqf and equipweapons.sqf from my preset loadouts v 1.44, a little credit if you do and its all good, my scripts rely ofc on arrays as you maybe know and may not be usable in configs, i have no idea about configs actually.

But it have been tested and bughunted very well by forum members and myself, most if not all flaws have been ironed out.

None known at this time when used with, vanilla, ACE, ACRE

---------- Post added at 04:09 AM ---------- Previous post was at 03:57 AM ----------

this worked:

using doubles but infront of comma in string.

	thearray = ["mygun1","mygun2","mygun3"];

_string = "";

for "_i" from 0 to (count thearray) -1 do
{
    if (_i == ((count thearray) -1)) then 
    {
        _string1 = format ["""%1""", thearray select _i];
        _string = _string + _string1;
    }else 
    {
        _string1 = format ["""%1"",", thearray select _i];
        _string = _string + _string1;
    };
};

it produced:

"mygun1","mygun2","mygun3"

Edited by Demonized

Share this post


Link to post
Share on other sites

wouldn't this be more efficient:

_MagazinesRuckList = [["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]];

_tempRuckMagList = [];

{
_tempRuckMagList set [count _tempRuckMagList, _x select 0];

} forEach _MagazinesRuckList;

or even this alternative if you keep getting the object instead of the string:

_MagazinesRuckList = [["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]];

_tempRuckMagList = [];

{
_tempRuckMagList set [count _tempRuckMagList, str _x select 0];

} forEach _MagazinesRuckList;

Share this post


Link to post
Share on other sites

@Demonized

Yes that is exactly what I did to get it working, Sorry I didn't update it sooner, Got it working earlier tonight.

@Gamadust

unfortunately that will not work, that will only give you one mag, it needs to be iterated over each inner array. to change it into a normal array, then I am producing a formated string, just like in Demonized's example above.

Thanks for all the help guys, I have it working now!

.

Share this post


Link to post
Share on other sites

...

unfortunately that will not work, that will only give you one mag, it needs to be iterated over each inner array. to change it into a normal array, then I am producing a formated string, just like in Demonized's example above.

Thanks for all the help guys, I have it working now!

.

That is strange! I just checked my code (I was using exacly that for similar purposes) but in doubt I simply run the exact code I posted, after this:

_MagazinesRuckList = [["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]];

_tempRuckMagList = [];

{
_tempRuckMagList set [count _tempRuckMagList, _x select 0];

} forEach _MagazinesRuckList;

player globalChat format["%1", _tempRuckMagList];

I got exacly this in the chat:

BLUFOR (GAMMA): "["30RND_556X45_STANAG","SMOKESHELL","HANDGRENADE_WEST"]"

if somehow this forEach is not iterating and adding each time only the first element to the temporary array... something is wrong with your array at runtime, it does not contain exacly what you stated in the first post.

just try it out... your arma engine won't result differently then mine

Share this post


Link to post
Share on other sites

@gammadust

I use ACE, in there backpack system, when I pull a players backpack contents I get a multidimensional array, like this:

[["30Rnd_556x45_Stanag",2],["SmokeShell",1],["HandGrenade_West",1]]

Now for my addon I need to convert that into a single regular array, so I need to turn the above into this:

["30Rnd_556x45_Stanag","30Rnd_556x45_Stanag","SmokeShell","HandGrenade_West"]

My code that I posted in my first post works, after the simple syntax error was corrected.

We kind of veired off talking about one of the other problems I had as well, that was posted in another thread. Sorry for the confusion.

Here is a confirmed working script:

_MagazinesRuckList = [p1] call ACE_fnc_RuckMagazineslist;

_tempRuckMagList = [];

{
   _tempnum = _x select 1;
   _tempclass = _x select 0;
   for [{_i=0}, {_i<_tempnum}, {_i=_i+1}] do
   {
       _tempRuckMagList = _tempRuckMagList + [_tempclass];
   };
} forEach _MagazinesRuckList;


_string = "";

for "_i" from 0 to (count _tempRuckMagList) -1 do
{
   if (_i == ((count _tempRuckMagList) -1)) then 
   {
       _string1 = format ["""%1""", _tempRuckMagList select _i];
       _string = _string + _string1;
   }else 
   {
       _string1 = format ["""%1"",", _tempRuckMagList select _i];
       _string = _string + _string1;
   };
};

Hint "You can now tab out and copy this to the userconfig file.";
copyToClipboard _string;

This script pulls the users backpack data converts the array and strings the data into a format that I can use, and copies it to the clipboard.

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  

×