Need advice on coding this function idea

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 983 Views - Last Post: 28 November 2010 - 12:24 PM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Need advice on coding this function idea

Posted 27 November 2010 - 06:52 AM

NOTE: Possibly long and confusing :(

I have an array, of the following, but it could be of any length

$array = array();
$array[] = 'a';
$array[] = 'b';
$array[] = 'c';
$array[] = 'd';
$array[] = 'e';
$array[] = 'f';
$array[] = 'g';
$array[] = 'h';
$array[] = 'ab';
$array[] = 'cd';
$array[] = 'ef';
$array[] = 'gh';
$array[] = 'abcd';
$array[] = 'efgh';
$array[] = 'abcdefgh';
$array[] = 'prevValue';
$array[] = 'prevValue+abcdefgh';



Here it is in tree form structure, and I need to walk the array in this way in my function

Posted Image

For example, if I have a function and I want to compute a walk from a specified value in the tree (lets say 'c'), it will walk the tree taking the values 'd' to the right of c, 'ab' to the top left on the upper level of c, 'efgh', to the top right of 'c' at the next level after that, and then the value 'prevValue' on the very top left.

So, for 'c', it's going left or right as you go up a level.

But depending on the specified document location in the tree diagram, it cannot always 'walk' the tree in this exact fashion.

Example, what if 'g' was selected?
This would be computed going to 'h', then 'ef', then 'abcd', then 'prevValue+abcdefgh'

'b' would be
'a', then 'cd', then 'efgh', then 'prevValue';


How can I code this dynamically corresponding to the location of each document, and if the number of values in the array will always be different??

Here is text that may/may not explain what I am trying to do better.
L = left, R = right.
http://i61.photobuck...jumper/text.jpg

Is This A Good Question/Topic? 0
  • +

Replies To: Need advice on coding this function idea

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 07:41 AM

What have you tried?
Without knowing what you've done and therefore where your problem lies, the best I can offer is array_walk.

This post has been edited by CTphpnwb: 27 November 2010 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 08:47 AM

I tried to hardcode the following for specified value 'c', but it did not move correctly through the array. I have no idea how to walk the 'tree' dynamically. I will try out array_walk though. Here is what I tried

Note: Here I already know the indexpos of the value 'c', and am walking the array based on that.

function computeWalk($indexPos){
$treeHash = $arrayOfHashes[$indexPos];
if ( $indexPos % 2 == 0 ) //if pos is on LEFT 
{
$treeHash .= $arrayOfHashes[$indexPos+1]; //concatenate with value 'd' on the right next to it
$treeHash .= $arrayofHashes[$indexPos+6]; //concatenate with value 'ab'
$treeHash .= $arrayOfHashes($indexPos+11]; //concatenate with value 'efgh';
$treeHash .= $arrayOfHashes($indexPos+13]; //concatenate with value 'prevValue';
}

else //If On Right
{
// do work
}

return $treeHash;
}



This is AWFUL I am aware. However it wasn't even concatenating correctly, just kept returning 'cd'.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 08:59 AM

$treeHash = $arrayOfHashes[$indexPos];

What is $arrayOfHashes? It is not defined in or passed to the function.

These are wrong: ( ] should be [ ].
$treeHash .= $arrayOfHashes($indexPos+11]; //concatenate with value 'efgh';
$treeHash .= $arrayOfHashes($indexPos+13]; //concatenate with value 'prevValue';


Was This Post Helpful? 0
  • +
  • -

#5 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 09:32 AM

Sorry.

This is my "test.php" page I created

$array = array();
$array[] = 'a';
$array[] = 'b';
$array[] = 'c';
$array[] = 'd';
$array[] = 'e';
$array[] = 'f';
$array[] = 'g';
$array[] = 'h';


computeWalk($array, 2);




function computeWalk($array, $indexPos){
$treeHash = $array[$indexPos];
if ( $indexPos % 2 == 0 ) //if pos is on LEFT 
{
$treeHash .= $array[$indexPos+1]; //concatenate with value 'd' on the right next to it
$treeHash .= $array[$indexPos+6]; //concatenate with value 'ab'
$treeHash .= $array[$indexPos+11]; //concatenate with value 'efgh';
$treeHash .= $array[$indexPos+13]; //concatenate with value 'prevValue';
}

else //If On Right
{
// do work
}

return $treeHash;
}


Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 11:07 AM

I'm not really sure what you're trying to do, but this should be closer:
<?php
$array = array();
$array[] = 'a';
$array[] = 'b';
$array[] = 'c';
$array[] = 'd';
$array[] = 'e';
$array[] = 'f';
$array[] = 'g';
$array[] = 'h';

$hash = computeWalk($array, 3);
print_r($array);

function computeWalk(&$array, $depth){
	$N = count($array);
	for($indexPos = 0; $indexPos < $N; $indexPos++)
	{
		for($i = $N; $i < $depth * $N; $i ++)
		{
			if ( $indexPos % 2 == 0 ) //if pos is on LEFT 
			{
				$array[$i] = $array[$i-$N].$array[$i-$N+4].$array[$i-$N +5];
			} else {
			//If On Right
			// do work
			}
		}
	}
}
?>

Was This Post Helpful? 1
  • +
  • -

#7 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Need advice on coding this function idea

Posted 27 November 2010 - 01:17 PM

I'm not sure why you're travelling the tree in the way you describe, but algorithmically I can suggest the following based on your description:

Get the string length of the last array entry.
Construct a for loop that iterates from 1 to the length but increments by multiplying the loop control by 2.
Using the loop control variable test the strings for the presence of the character you are seeking out. Looking in each of the strings whose length is now specified by the loop control variable.
Return the values that do not contain the specified character.
Was This Post Helpful? 1
  • +
  • -

#8 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 27 November 2010 - 01:49 PM

Thanks for all your advice, I will try it out :)
Was This Post Helpful? 0
  • +
  • -

#9 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Need advice on coding this function idea

Posted 27 November 2010 - 08:36 PM

This SHOULD do it, thought it is completely unrelated to the algorithm I specified earlier because it simply traverses the whole array in a very brute force sort of way. Furthemore, it only works in a case where the tree is even.
<?php
// Be sure to include your $array values here as in your first post
$result = array();
$needle = 'c';
for($i = 0; $i < count($value) - 1; $i++){
	if(false !== strpos($array[$i], $needle)){
		if($i % 2 == 0){
			$result[] = $array[$i + 1];
		}else{
			$result[] = $array[$i - 1];
		}
	}
}
print_r($result);
?>

This post has been edited by grimpirate: 27 November 2010 - 08:39 PM

Was This Post Helpful? 1
  • +
  • -

#10 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 28 November 2010 - 03:43 AM

Thank you, I am trying these out today, I will report back :)
Was This Post Helpful? 0
  • +
  • -

#11 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 28 November 2010 - 04:03 AM

I coded the above as

$array = array();
$array[] = 'a';
$array[] = 'b';
$array[] = 'c';
$array[] = 'd';
$array[] = 'e';
$array[] = 'f';
$array[] = 'g';
$array[] = 'h';
echo computeWalk($array);



function computeWalk($array){
$result = array();
$needle = 'c';
for($i = 0; $i < count($array) - 1; $i++){
	if(false !== strpos($array[$i], $needle)){
		if($i % 2 == 0){
			$result[] = $array[$i + 1];
		}else{
			$result[] = $array[$i - 1];
		}
	}
}
print_r($result);
}



but I get the result
Array ( [0] => d ) 



Determined to keep trying!!! Will post more things I come up with.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Need advice on coding this function idea

Posted 28 November 2010 - 05:14 AM

Look at the differences between my code and yours:
  • Your first if statement doesn't exist in my code.
  • I change $array by concatenating 3 of its elements and adding them to the array.
  • You create a new array by setting its value(s) to those of $array when conditions meet your criteria.

Was This Post Helpful? 0
  • +
  • -

#13 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: Need advice on coding this function idea

Posted 28 November 2010 - 08:32 AM

When I run your function, it prints

Quote

Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => aef [9] => bfg [10] => cgh [11] => dhaef [12] => eaefbfg [13] => fbfgcgh [14] => gcghdhaef [15] => hdhaefeaefbfg [16] => aefeaefbfgfbfgcgh [17] => bfgfbfgcghgcghdhaef [18] => cghgcghdhaefhdhaefeaefbfg [19] => dhaefhdhaefeaefbfgaefeaefbfgfbfgcgh [20] => eaefbfgaefeaefbfgfbfgcghbfgfbfgcghgcghdhaef [21] => fbfgcghbfgfbfgcghgcghdhaefcghgcghdhaefhdhaefeaefbfg [22] => gcghdhaefcghgcghdhaefhdhaefeaefbfgdhaefhdhaefeaefbfgaefeaefbfgfbfgcgh [23] => hdhaefeaefbfgdhaefhdhaefeaefbfgaefeaefbfgfbfgcgheaefbfgaefeaefbfgfbfgcghbfgfbfgcghgcghdhaef )


:S

This post has been edited by megglz: 28 November 2010 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,885
  • Joined: 08-August 08

Re: Need advice on coding this function idea

Posted 28 November 2010 - 09:13 AM

So you should be able to modify it to produce what you're looking for.
Was This Post Helpful? 1
  • +
  • -

#15 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Need advice on coding this function idea

Posted 28 November 2010 - 12:20 PM

This is the code you should've run megglz, which is why I stated the array from your original post. I remember posting a while back a code which would return the levels of the tree. So basically, if you're given an array of single characters, you would run that code first to make the array into the first portion of the code and then you would run this code to determine the navigation through the tree.
<?php
$array = array();
$array[] = 'a';
$array[] = 'b';
$array[] = 'c';
$array[] = 'd';
$array[] = 'e';
$array[] = 'f';
$array[] = 'g';
$array[] = 'h';
$array[] = 'ab';
$array[] = 'cd';
$array[] = 'ef';
$array[] = 'gh';
$array[] = 'abcd';
$array[] = 'efgh';
$array[] = 'abcdefgh';

print_r(computeWalk($array));

function computeWalk($array){
	$result = array();
	$needle = 'c';
	for($i = 0; $i < count($array) - 1; $i++){
		if(false !== strpos($array[$i], $needle)){
			if($i % 2 == 0){
				$result[] = $array[$i + 1];
			}else{
				$result[] = $array[$i - 1];
			}
		}
	}
	return $result;
}
?>

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2