PDA

View Full Version : Arrays, functions, breaks and basic programmin logic question

matak
06-03-2007, 03:58 AM
I cant figure how to escape function which creates array, and then goes to antoher function. Problem is that function calls that other function X times the array is created. Here's the logic

function guess_where(\$1,\$2,\$3) {
// for() function guesses numbers where 2, 5, 10, etc..
//
create_array(\$1) //calling a function what creates array from numbers found
}

function create_array(\$1){
GLOBAL \$2;
GLOBAL \$3;

//the part where array is created

use_array(\$array, \$2, \$3)
}

function use_array(\$array, \$2, \$3){

//beacouse of the logic of the first function this function is egzecuted
//X number of times depending on the size of the array

}

I can't figure out, how to CALL a function, only when the whole array is crated, instead that it goes through whole FOR loop calling create_array function every time..

Here's my guess_where function logic

function guess_where(\$1,\$2,\$3) {
for(\$a; \$a < sizeof(\$2; \$a++)

\$term1 //1st term
\$term2 //2nd term

if (\$term1 == \$term2):
create_array(\$1);
endif;

}

Hope this isn't bothering you too much. And thanks for the help!

ralph l mayo
06-03-2007, 06:15 AM
This is possibly the worst code I've ever seen :O

What are you trying to accomplish, in broad terms?

matak
06-03-2007, 06:48 AM
a function that will work on something, and when it doesn't have any more values to work with start another function...

ralph l mayo
06-03-2007, 08:09 AM
a function that will work on something, and when it doesn't have any more values to work with start another function...

Start work with the same array you mean?

function do_some_stuff_to_array(\$array)
{
foreach (\$array as &\$val)
{
// stuff == double each entry
\$val *= 2;
}
return do_some_more_stuff_to_array(\$array);
}

function do_some_more_stuff_to_array(\$array)
{
foreach (\$array as &\$val)
{
// more stuff == decrement each entry
--\$val;
}
return \$array;
}

print_r(do_some_stuff_to_array(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)))
// Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => 11 [6] => 13 [7] => 15 [8] => 17 [9] => 19 )

Post again if that's not what you're driving at... You went a little too broad for me :P I was trying to discover the purpose of all this in concrete terms. Like "I want to transform an array of integers into the sum of its elements' squares," but not exactly that, unless it's really what you want.

matak
06-03-2007, 09:08 AM
I'm just trying to improve :)

Also i think i have managed to solve the problem. Just using google for now to test my beginners search script, which needs a loooot more improvement.