Problem with simple data structure.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Problem with simple data structure.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you gone through it step-by-step on paper?
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post 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"?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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?
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post 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;".
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post by nadavvin »

Is it not possible to return reference instead of copy?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post 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)?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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)
    }
  }
}
*/
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post by nadavvin »

Thanks very much :D

Code: Select all

function &go(&$a,$count)

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