Strings and Arrays

Posted By: SomebodyNew

Strings and Arrays - 09/15/09 08:13

Hello there,

I'm trying to construct the name of an array out of several Strings using str_cat . And then use the resulting string (the name of the array) to modify the array's values.

For Example:
Code:
var probe_1[4] = { 1, 1, 1, 1};
var probe_2[4] = { 2, 2, 2, 2};
var probe_3[4] = { 3, 3, 3, 3};

var test[4] = { 0, 0, 0, 0};

STRING* first_half = "probe_";
STRING* number;
STRING* second_half = "[3]";



Now I use the following function
Code:
function main()
{
	str_cpy(number, "1");
	str_cat(first_half, number);      // "probe_" + "1"
	str_cat(first_half, second_half); // "probe_1" + "[3]"
	test[3] = first_half;
}



But... it doesn't work. When I check for the value of test[3] it gives me a really huge number (90688).
The problem appears to be in the last line. It appears that the string "first_half" is not recognized as an array.

I'm stuck here and even after checking the manual I don't know how to work around this. What did I miss?
Any help is appreciated. smile
Posted By: DJBMASTER

Re: Strings and Arrays - 09/15/09 09:32

You can't do that in lite-c. You are trying to evaluate a string so that the compiler see's it as the value at probe_1[3]. What you are actually doing is setting the 3rd position of the test array to the string "probe_1[3]".

Have you completed the lite-c workshops? You need to understand how arrays work before diving into them because they can be quite complex at times.
Posted By: SomebodyNew

Re: Strings and Arrays - 09/15/09 11:43

Thanks for your answer DJBMASTER.
Yes, of course I have completed the lite-c workshops. And I just reread them. Was my problem really that obvious?
I thought about using a 3*4 matrice for the values stored in probe_1 to probe_3.
Then there is the problem of filling the matrice with numbers (since you can't do it in the definition).
I used an algorithm that reads the values of probe_1 and fils them into a new matrice, then does the same for probe_2 and so on.

So, in general, is there a function that allows to write an algorithm that looks for a specific value at a specific position in each array?
like: <is probe_1[3] = 5?> and does it for all arrays up to probe_500 (or any big number)?
(I would very much prefer to not load all those arrays into one matrice).
Posted By: DJBMASTER

Re: Strings and Arrays - 09/15/09 17:26

Why can't you fill it with numbers in the definition? A matrix is just a multidimensional array so you will have...
Code:
var probe_matrix[3][4] = {1, 1, 1, 1,
                          2, 2, 2, 2,
                          3, 3, 3, 3
                         };



You can use a simple for loop and use if statements to check the value of each index.
Posted By: SomebodyNew

Re: Strings and Arrays - 09/16/09 12:12

As far as I know (i hope somebody will correct me here) lite-c doesn't allow you to fill a matrice with numbers in the definition (well it does, but you can't use more than one line. So have fun with something like a 30*40 matrice).
That means you have to divide the matrice into arrays. Each line of the matrice is represented by a array. And only now you can use an algorithm that reads the arrays and writes them in the matrice.
So you end up with arrays + matrice. While they're basically the same.

Thats why I tried to avaoid using matrices.
It does work - but is a major inconvenience.

c#/c++ allow the type of declaration you mentioned.



A sidequestion: is there a way one can display a matrice's values ingame? Like you can do with an array's values. Panel -> digits ... They don't work with multidimensional arrays.

I'm using lite-C in version 7.11.1.
They added some things to matrices in version 7.80 i think. But I don't know what it is or if the updates would affect this issue.
© 2024 lite-C Forums