dnk 13 Posted January 13, 2011 foglist = ["a","a_1"]; for [{i=2},{i < 99},{i = i +1}] do {foglist = foglist + ["a_i"]; }; then a {}foreach foglist; that requires input like this "var". I can get the foreach to work without the for/do, but the for/do messes everything up (including the original two variables). Share this post Link to post Share on other sites
fatty86 10 Posted January 13, 2011 (edited) I got it working using format to convert the variable into a string, then concatenating it with 'a': foglist = ["a","a_1"]; for [{i=2},{i < 99},{i = i +1}] do {foglist = foglist + ["a_" + format ["%1",i]];}; That produced an array where foglist = ["a","a_1","a_2",..."a_98"]. I assume that's what you were after? Edited January 13, 2011 by fatty86 Share this post Link to post Share on other sites
GDICommand 10 Posted January 13, 2011 (edited) foglist = ["a","a_1"]; for [{i=2},{i < 99},{i = i +1}] do {foglist = foglist + ["a_i"]; }; then a {}foreach foglist; that requires input like this "var". I can get the foreach to work without the for/do, but the for/do messes everything up (including the original two variables). thats not how you access an element in an array, what you want is this: foglist = ["a","a_1"]; for [{i=0},{i < count foglist},{i = i +1}] do { hint format["value at index %1 of foglist array: %2", i, (foglist select i)]; }; that just prints out the index you are evaluating and the string. i corrected a few things for you. NOTE: i recommend you do some more reading about arrays, and the specific syntax that arma requires. my explanation is just identifying your mistakes, nothing more first off, you were evaluating the for loop over indexes that don't exist inside the array. so, when your loop iterated to i equals 3, then you would get IndexOutOfBoundsException basically, which means, that the index you are trying to access is invalid. so, if you notice in the second set of brackets in the for loop, i set it to iterate only to the number of elements in the array, and then break out of the loop by using count. you should always use things such as count wherever possible. writing anything statically in, is just asking for whatever you wrote to break. imagine a program (an actual program), where you reference a value a couple dozen times, lets just call it 24 times. now imagine that value changes, if you statically write in that variable, now you have to change the 24 occurrences manually, which if you don't remember where they all are, or your program spans across multiple files, then you may forget one or two, and now your program doesnt work, and you could spend days trying to figure out why. if you use count, in this particular case, you could have 24 for loops, and maybe you want to add an index, now nothing breaks, because each for loop will iterate up to the last index of the array, and all you did was add an index secondly, assuming we fix the count issue first, you were only evaluating one index of the array. so I set the initial value for i to equal 0, instead of 2. if you only want one index of the array, and you know the index already, then the for loop becomes unnecessary, and when it breaks down to into machine instructions (if not optimized) then it will actually consume more cpu cycles. if you are only interested in one particular index, then you will do it as such: hint format["value at index %1 of foglist array: %2", 2, (foglist select 2)]; by statically writing in the index you are attempting to access EDIT: { I think you were trying to add an index rather than access existing indexes...in any case, I still think you should do some reading for reasons I wont elaborate on (because I think its beyond the scope of this thread), you should really construct two separate arrays. one which is your original array, and the other which contains all the new values you want to add to the old array. after ALL your values have been added to the new array, then you can join the two together } Edited January 13, 2011 by GDICommand Share this post Link to post Share on other sites
-)rStrangelove 0 Posted January 13, 2011 ...then you can join the two together} And that's what he does - adding 2 arrays into 1: ;) foglist = foglist + ["a_" + format ["%1",i]]; Share this post Link to post Share on other sites
dnk 13 Posted January 13, 2011 Sorry for not explaining what I was trying to do, that was stupid of me to omit. I am trying to save myself a lot of copying, pasting, deleting, and number-typing. I want to take an array of two values (a and a_1) and add to that values a_2 through a_98 (probably too many, but we'll see). Then take that array and put it through a different "foreach" loop that executes an FSM for all elements (that is not depicted, all that you need to know is I need the element string (of a marker) for the second foreach). Anyway, thank you for the input. Where can I read on this? I read the wiki on arrays but got a bit confused as it's fairly simple. I mean, I see nothing about "%"s and "value" commands and "format" commands in the wiki entry there (although I'll be sure to read up on them tomorrow). They definitely are my weak point. I spent a lot of time today figuring out that this: {..... [_x] execVM "blah.sqf"; } foreach allunits; requires you to do a "_this select 0;" in the next file; you can't just treat "_this" like an object (I still don't understand why, but okay). ---------- Post added at 10:40 AM ---------- Previous post was at 10:38 AM ---------- Anyway, scripting is a fun way to waste a half hour realizing that what seems completely logical is actually, in fact, gibberish, despite still seeming completely rational. Share this post Link to post Share on other sites
st_dux 26 Posted January 13, 2011 requires you to do a "_this select 0;" in the next file; you can't just treat "_this" like an object (I still don't understand why, but okay). You have to use "_this select 0" in your example because you are passing _x in an array (hence the brackets). If you were to call the script like this -- "_x execVM 'blah.sqf'" -- then "_this" would work as expected. Share this post Link to post Share on other sites
dnk 13 Posted January 13, 2011 oooooh. I thought you had to format it as [] execVM ""; No? Apparently not, I guess. The wiki maybe is misleading sometimes, or maybe I don't read thoroughly enough. Confirmed: I don't read thoroughly enough :) It is nice when I actually understand why something doesn't work for a change. I should come here often. Share this post Link to post Share on other sites
Deadfast 43 Posted January 13, 2011 oooooh. I thought you had to format it as [] execVM "";No? Apparently not, I guess. The wiki maybe is misleading sometimes, or maybe I don't read thoroughly enough. You can pass any data type to the execVM'd script. When passing a single element there is absolutely no need to pass it as an array with a single element. Share this post Link to post Share on other sites
dnk 13 Posted January 13, 2011 Yes, now I know. It's just every time I saw it used in an example or something, people put the [] before it for some reason. Share this post Link to post Share on other sites
Deadfast 43 Posted January 13, 2011 Yes, now I know. It's just every time I saw it used in an example or something, people put the [] before it for some reason. I know, it's an extremely common misconception. I think people just believe the [] is a part of the command. Share this post Link to post Share on other sites