Page 1 of 1

Problem with simple data structure.

Posted: Fri Feb 23, 2007 2:48 pm
by nadavvin

Code: Select all

$a=array();
$a[0]=1;
$a[1]=array();
$a[1][0]=5;
$a[1][1]=array();
$a[1][1][0]=8;
This is work:

Code: Select all

function go(&$a,$count)
{
	$arr=&$a;
	for($i=0;$i<$count;$i++)
	{
		echo "here\n";
		$arr=&$arr[1];
	}
	$arr[0]=6;
	return $arr;
}
$arr=&go(&$a,3);
var_dump($a);
but this is not work:

Code: Select all

function go(&$a,$count)
{
	$arr=&$a;
	for($i=0;$i<$count;$i++)
	{
		echo "here\n";
		$arr=&$arr[1];
	}
	
	return $arr;
}
$arr=&go(&$a,3);
$arr[0]=6;
var_dump($a);
What is the problem?
Why six is not added in the second code?

Posted: Fri Feb 23, 2007 9:19 pm
by feyd
Have you gone through it step-by-step on paper?

Posted: Sat Feb 24, 2007 1:15 am
by nadavvin
feyd wrote:Have you gone through it step-by-step on paper?
Sorry, I don't understand what to to do from your response.

What is the "pager"?

Posted: Sat Feb 24, 2007 4:41 am
by Mordred
It is p a p e r, not p a [s]g[/s] e r. You know, the material of which dictionaries are made.

I declare this code is an Abomination unto Nuggan, I suggest you delete it and try something else instead ;) What exactly are you trying to do?

Posted: Sat Feb 24, 2007 4:56 am
by nadavvin
What exactly are you trying to do?
I have task in my data structures course so I need to implement all this myself.

I have some methods which get or set data, therefore to reduce common code, I want this method which only go to the node and give reference to it.

I guess the problem is that a copy of the array from the same found place is return instead get the reference of it.

I can't do "return &$arr;".

Posted: Sun Feb 25, 2007 2:55 pm
by nadavvin
Is it not possible to return reference instead of copy?

Posted: Sun Feb 25, 2007 3:02 pm
by Ambush Commander
Ah, you already found the problem. If copy v reference is the only trouble (I can't understand the algorithm), do:

Code: Select all

function &go(&$a,$count)
I recommend that you document your algorithm, because Mordred doesn't understand it. ;-) Ah, the beauty of Computer Science.

Posted: Sun Feb 25, 2007 3:17 pm
by nadavvin

Code: Select all

function go(&$a,$count)
{
	/**
	* return reference to a "node" who place in $count steps 
	* from the beging.
	*
	* &$a - reference to array to prevent copy of it.
	* $count - number of step to move.s
	*/
	$arr=&$a;  //define local var to move in the array.
			   //and start in first "node".
	for($i=0;$i<$count;$i++)
	{
		$arr=&$arr[1]; //set the local var to point the next array.
	}
	return $arr; //return the desire reference
}
$arr=go($a,2); //go two step
$arr[0]=6;		//set the value of it to 3.

var_dump($a);
and It's not work.

Why 6 is not appear in var_dump($a)?

Posted: Sun Feb 25, 2007 3:26 pm
by Ambush Commander
Given your documentation, I've made a few edits to the code.

Code: Select all

function &getNodeAtDepth(&$a, $count)
{
        /**
        * Return reference to the node that is $count levels
        * deep.
        *
        * $a - array to process, in format of array(0=>integer, 1=>array)
        * $count - number of levels to step down
        */
        $arr =& $a; // define local reference to array
        for($i=0; $i < $count; $i++)
        {
                $arr =& $arr[1]; //set the local var to point the next array.
        }
        return $arr; //return the desired reference
}
$a=array();
$a[0]=1;
$a[1]=array();
$a[1][0]=5;
$a[1][1]=array();
$a[1][1][0]=8; 
$arr =& getNodeAtDepth($a, 2); // go two depths down: NEEDS & operator
$arr[0] = 6; //set the value

var_dump($a);
/*
expect:
array(2) {
  [0]=>
  int(1)
  [1]=>
  &array(2) {
    [0]=>
    int(5)
    [1]=>
    &array(1) {
      [0]=>
      int(6)
    }
  }
}
*/

Posted: Sun Feb 25, 2007 3:36 pm
by nadavvin
Thanks very much :D

Code: Select all

function &go(&$a,$count)

$arr=&go($a,1); //go three step
Nadav