problem returning value using recursive function
Hello, I have the following problem:
I want my function to return a value, $l, using the following function:
function level($n,$l=0,$d=false) {
if ($d) { return $l; }
else {
$ff=get_folders();
for ($i=0;$i<count($ff);$i++) {
if ($n==$ff[$i][0]) {
if ($ff[$i][4]!=0) {
$l++;
level($ff[$i][4],$l,$d);
}
else {
$d=true;
level($ff[$i][0],$l,$d);
}
}
}
}
}
The function does not return $l, however. If I replace "return $l" in
the first line of the function with "print $l", the right value for $l
appears on the screen.
Does anyone know a solution to this problem?
Best,
Rienk
Re: problem returning value using recursive function
Rienk Withaar wrote:
> Hello, I have the following problem:
>
> I want my function to return a value, $l, using the following function:
>
> function level($n,$l=0,$d=false) {
> if ($d) { return $l; }
> else {
> $ff=get_folders();
> for ($i=0;$i<count($ff);$i++) {
> if ($n==$ff[$i][0]) {
> if ($ff[$i][4]!=0) {
> $l++;
> level($ff[$i][4],$l,$d);
> }
> else {
> $d=true;
> level($ff[$i][0],$l,$d);
> }
> }
> }
> }
> }
>
> The function does not return $l, however. If I replace "return $l" in
> the first line of the function with "print $l", the right value for $l
> appears on the screen.
>
> Does anyone know a solution to this problem?
>
> Best,
>
> Rienk
What do you get from the function when you use 'return', what value does
it give? Can you show us any more of the code.
Re: problem returning value using recursive function
"Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
news:evl3vj$abq$2 [at] info.service.rug.nl...
> Hello, I have the following problem:
>
> I want my function to return a value, $l, using the following function:
>
> function level($n,$l=0,$d=false) {
> if ($d) { return $l; }
> else {
> $ff=get_folders();
> for ($i=0;$i<count($ff);$i++) {
> if ($n==$ff[$i][0]) {
> if ($ff[$i][4]!=0) {
Post-incrementing here will cause$1 to be incremented by 2 on the next
recursion.
If on the first pass $1 = 0, on the second pass it will be 2, not 1.
If that is your goal, I've not found a problem ;)
> $l++;
> level($ff[$i][4],$l,$d);
> }
> else {
> $d=true;
> level($ff[$i][0],$l,$d);
> }
> }
> }
> }
> }
>
HTH
Vince
Re: problem returning value using recursive function
"Tyno Gendo" <you [at] localhost> wrote in message
news:ruSdnXSpyIqqtYPbnZ2dnUVZ8qTinZ2d [at] bt.com...
> Rienk Withaar wrote:
> > Hello, I have the following problem:
> >
> > I want my function to return a value, $l, using the following function:
> >
> > function level($n,$l=0,$d=false) {
> > if ($d) { return $l; }
> > else {
> > $ff=get_folders();
> > for ($i=0;$i<count($ff);$i++) {
> > if ($n==$ff[$i][0]) {
> > if ($ff[$i][4]!=0) {
> > $l++;
> > level($ff[$i][4],$l,$d);
> > }
> > else {
> > $d=true;
> > level($ff[$i][0],$l,$d);
> > }
> > }
> > }
> > }
> > }
Having taken a quick look at this it seems that the variable $d is never set
to true unless it is set at true for the original call of the function.
Re: problem returning value using recursive function
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461e2c62$0$16555$afc38c87 [at] news.optusnet.com.au...
> "Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
> news:evl3vj$abq$2 [at] info.service.rug.nl...
> > Hello, I have the following problem:
> >
> > I want my function to return a value, $l, using the following function:
> >
> > function level($n,$l=0,$d=false) {
> > if ($d) { return $l; }
> > else {
> > $ff=get_folders();
> > for ($i=0;$i<count($ff);$i++) {
> > if ($n==$ff[$i][0]) {
> > if ($ff[$i][4]!=0) {
>
> Post-incrementing here will cause$1 to be incremented by 2 on the next
> recursion.
Sorry, that should read "when you enter the next recursion".
Re: problem returning value using recursive function
"Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
news:evl3vj$abq$2 [at] info.service.rug.nl...
> Hello, I have the following problem:
>
> I want my function to return a value, $l, using the following function:
>
> function level($n,$l=0,$d=false) {
> if ($d) { return $l; }
> else {
> $ff=get_folders();
> for ($i=0;$i<count($ff);$i++) {
> if ($n==$ff[$i][0]) {
> if ($ff[$i][4]!=0) {
> $l++;
> level($ff[$i][4],$l,$d);
> }
> else {
If you set $d to true here you may return $1 at this point rather than
doing another recursion and catching it there.
Not that it's an error of course, it's just a suggestion.
> $d=true;
> level($ff[$i][0],$l,$d);
> }
> }
> }
> }
> }
Re: problem returning value using recursive function
"Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
news:evl3nn$abq$1 [at] info.service.rug.nl...
| Hello, I have the following problem:
|
| I want my function to return a value, $l, using the following function:
|
| function level($n,$l=0,$d=false) {
| if ($d) { return $l; }
| else {
| $ff=get_folders();
| for ($i=0;$i<count($ff);$i++) {
| if ($n==$ff[$i][0]) {
| if ($ff[$i][4]!=0) {
| $l++;
| level($ff[$i][4],$l,$d);
| }
| else {
| $d=true;
| level($ff[$i][0],$l,$d);
| }
| }
| }
| }
| }
|
| The function does not return $l, however. If I replace "return $l" in
| the first line of the function with "print $l", the right value for $l
| appears on the screen.
|
| Does anyone know a solution to this problem?
you could do with a much smaller 'nest'...
function level($n, $l = 0, $d = false)
{
if ($d){ return $l; }
// no need to have an else here
// get_folders();
// loop
// no need to have if nest here...
// if (!condition){ continue; }
// will suffice.
// no need at all for $d since
// you call the function again needlessly
// just so you can return $l from the
// previous iteration
}
i'll get to your answer shortly...there are more important things going on
here.
there are too many needless brackets and none of them are aligned. couple
that with meaningless variable names, and you've got the making of code that
is hard to debug and maintain. hmmm, that may lead to a post in a usenet
group. ;^)
consider this:
function level($n, $l = 0)
{
$ff = get_folders();
for ($i = 0; $i < count($ff); $i++)
{
if ($n != $ff[$i][0]){ continue; }
if ($ff[$i][4] != 0)
{
$l++;
level($ff[$i][4], $l);
} else {
return $l;
}
}
}
at least now you're dealing with meaningless variable names...but you can
read the code easier.
i assume get_folders() returns the same set of folders on each call to
level(). this code is going to run like a dog if that's the case. it looks
like you're screening with level() just so that you know where to begin
working/processing...VERY inefficient.
to answer your problem specifically, level NEVER ASSIGNS the return value to
ANYTHING in ANY ITERATION. it's just not thought out or executed well. state
what you're trying to do and i'll help you do it...correctly.
AND FOR GAWD'S SAKE...DON'T MULTI-POST!!!
Re: problem returning value using recursive function
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461e2d34$0$15781$afc38c87 [at] news.optusnet.com.au...
|
| "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
| news:461e2c62$0$16555$afc38c87 [at] news.optusnet.com.au...
| > "Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
| > news:evl3vj$abq$2 [at] info.service.rug.nl...
| > > Hello, I have the following problem:
| > >
| > > I want my function to return a value, $l, using the following
function:
| > >
| > > function level($n,$l=0,$d=false) {
| > > if ($d) { return $l; }
| > > else {
| > > $ff=get_folders();
| > > for ($i=0;$i<count($ff);$i++) {
| > > if ($n==$ff[$i][0]) {
| > > if ($ff[$i][4]!=0) {
| >
| > Post-incrementing here will cause$1 to be incremented by 2 on the next
| > recursion.
| Sorry, that should read "when you enter the next recursion".
that's not quite true either...he needlessly enters the next recursion and
if $d, immediately returns w/o incrementing $l.
:)
Re: problem returning value using recursive function
"Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
news:evl3vj$abq$2 [at] info.service.rug.nl...
> Hello, I have the following problem:
>
> I want my function to return a value, $l, using the following function:
>
> function level($n,$l=0,$d=false) {
> if ($d) { return $l; }
> else {
> $ff=get_folders();
> for ($i=0;$i<count($ff);$i++) {
> if ($n==$ff[$i][0]) {
> if ($ff[$i][4]!=0) {
> $l++;
> level($ff[$i][4],$l,$d);
> }
> else {
> $d=true;
> level($ff[$i][0],$l,$d);
> }
> }
> }
> }
> }
>
function level($n, $1=0,$d=false){
$ff=get_folders();
$c=count($ff);
for($i=0;$i<$c){
if($n==$ff[$i][0]){
if($ff[$i][4]!=0){
level($ff[$i][0],$1,$d);
}
}
else{
return $1;
}
}
If($1 = "some upper limit"){
die('level() has recursed '.$1.' times without finding the folder')
}
}
Please excuse me taking the liberty of altering your function slightly. In
doing so it became apparent that there is a very real chance that this could
cause a stack overflow if there is no check for a complete failure to find
the folder, so I added one.
get_folders() is always going to return the same array on every recursion,
unless you make another one in an astoundingly short time, and should be
called from outside this function.
It's past my bed time so there could be some horrible errors in the above.
Vince
Re: problem returning value using recursive function
"Steve" <no.one [at] example.com> wrote in message
news:juqTh.10$G46.6 [at] newsfe04.lga...
> that's not quite true either...he needlessly enters the next recursion and
> if $d, immediately returns w/o incrementing $l.
>
> :)
>
>
Are you sure Steve?
function level($n,$l=0,$d=false) {
if ($d) { return $l; }
else {
$ff=get_folders();
for ($i=0;$i<count($ff);$i++) {
if ($n==$ff[$i][0]) {
if ($ff[$i][4]!=0) {
Here he increments again before calling level()
$l++;
level($ff[$i][4],$l,$d);
}
else {
But here he has found what he wants and it should return on the next
recursion.
$d=true;
level($ff[$i][0],$l,$d);
}
}
}
}
}
I could be wrong, it wouldn't be the first time ;)
Vince
Re: problem returning value using recursive function
"Steve" <no.one [at] example.com> wrote in message
news:juqTh.10$G46.6 [at] newsfe04.lga...
>
> "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
> news:461e2d34$0$15781$afc38c87 [at] news.optusnet.com.au...
> |
> | "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
> | news:461e2c62$0$16555$afc38c87 [at] news.optusnet.com.au...
> | > "Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
> | > news:evl3vj$abq$2 [at] info.service.rug.nl...
> | > > Hello, I have the following problem:
> | > >
> | > > I want my function to return a value, $l, using the following
> function:
> | > >
> | > > function level($n,$l=0,$d=false) {
> | > > if ($d) { return $l; }
> | > > else {
> | > > $ff=get_folders();
> | > > for ($i=0;$i<count($ff);$i++) {
> | > > if ($n==$ff[$i][0]) {
> | > > if ($ff[$i][4]!=0) {
> | >
I should have gone to bed long ago. You are clearly dead Right!!
This mess of var names has screwed with my head. I've see $i and $1 as the
same var!
My applogies for doubting you Steve, I should have known better.
Please excuse me Rienk.
Regards,
Vince
Re: problem returning value using recursive function
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461e36c1$0$5743$afc38c87 [at] news.optusnet.com.au...
| "Steve" <no.one [at] example.com> wrote in message
| news:juqTh.10$G46.6 [at] newsfe04.lga...
|
| > that's not quite true either...he needlessly enters the next recursion
and
| > if $d, immediately returns w/o incrementing $l.
| >
| > :)
| >
| >
|
| Are you sure Steve?
yes.
| function level($n,$l=0,$d=false) {
| if ($d) { return $l; }
| else {
| $ff=get_folders();
| for ($i=0;$i<count($ff);$i++) {
| if ($n==$ff[$i][0]) {
| if ($ff[$i][4]!=0) {
|
| Here he increments again before calling level()
true enough, however when he actually expects a return value rather than
recurse level() again, there is no need to call level(), and further, no
need for $d at all. the return should be immediate. notice again, however,
that level() never sets anything for return to return into?
| $l++;
| level($ff[$i][4],$l,$d);
| }
| else {
|
| But here he has found what he wants and it should return on the next
| recursion.
needlessly, true...but into what does he return $l from any iteration?
| $d=true;
| level($ff[$i][0],$l,$d);
| }
| }
| }
| }
| }
|
| I could be wrong, it wouldn't be the first time ;)
me too. i've lost count.
Re: problem returning value using recursive function
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:461e4047$0$16553$afc38c87 [at] news.optusnet.com.au...
| "Steve" <no.one [at] example.com> wrote in message
| news:juqTh.10$G46.6 [at] newsfe04.lga...
| >
| > "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
| > news:461e2d34$0$15781$afc38c87 [at] news.optusnet.com.au...
| > |
| > | "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
| > | news:461e2c62$0$16555$afc38c87 [at] news.optusnet.com.au...
| > | > "Rienk Withaar" <r.g.withaar [at] rug.nl> wrote in message
| > | > news:evl3vj$abq$2 [at] info.service.rug.nl...
| > | > > Hello, I have the following problem:
| > | > >
| > | > > I want my function to return a value, $l, using the following
| > function:
| > | > >
| > | > > function level($n,$l=0,$d=false) {
| > | > > if ($d) { return $l; }
| > | > > else {
| > | > > $ff=get_folders();
| > | > > for ($i=0;$i<count($ff);$i++) {
| > | > > if ($n==$ff[$i][0]) {
| > | > > if ($ff[$i][4]!=0) {
| > | >
|
| I should have gone to bed long ago. You are clearly dead Right!!
| This mess of var names has screwed with my head. I've see $i and $1 as
the
| same var!
| My applogies for doubting you Steve, I should have known better.
| Please excuse me Rienk.
| Regards,
| Vince
you crack me up. :)
l8r
Re: problem returning value using recursive function
| consider this:
|
| function level($n, $l = 0)
| {
| $ff = get_folders();
| for ($i = 0; $i < count($ff); $i++)
| {
| if ($n != $ff[$i][0]){ continue; }
| if ($ff[$i][4] != 0)
| {
| $l++;
| level($ff[$i][4], $l);
| } else {
| return $l;
| }
| }
| }
lemme practice what i preach (and usually do)...
function level($n, $l = 0)
{
$ff = get_folders();
for ($i = 0; $i < count($ff); $i++)
{
if ($n != $ff[$i][0]){ continue; }
if ($ff[$i][4] != 0)
{
level($ff[$i][4], ++$l);
}
return $l;
}
}
the difference? NO else statements (as none are needed, as they rarely are)
and $l is only incremented in the recursive calls to level()...which helps
more plainly show that it is only a change for parameter calls in level()
rather than massaged elsewhere in level().
either way, it's still shitty code...there's just less of it to cause a
stinch and is more readily identifiable as shit since it can be read and
understood at a glance.
Re: problem returning value using recursive function
"Steve" <no.one [at] example.com> wrote in message
news:_HrTh.2221$Xq6.1291 [at] newsfe12.lga...
|| consider this:
||
|| function level($n, $l = 0)
|| {
|| $ff = get_folders();
|| for ($i = 0; $i < count($ff); $i++)
|| {
|| if ($n != $ff[$i][0]){ continue; }
|| if ($ff[$i][4] != 0)
|| {
|| $l++;
|| level($ff[$i][4], $l);
|| } else {
|| return $l;
|| }
|| }
|| }
|
| lemme practice what i preach (and usually do)...
|
| function level($n, $l = 0)
| {
| $ff = get_folders();
| for ($i = 0; $i < count($ff); $i++)
| {
| if ($n != $ff[$i][0]){ continue; }
| if ($ff[$i][4] != 0)
| {
| level($ff[$i][4], ++$l);
| }
| return $l;
| }
| }
ooh, or how about this:
function level($n, $l = 0)
{
$ff = get_folders();
for ($i = 0; $i < count($ff); $i++)
{
if ($n != $ff[$i][0]){ continue; }
if ($ff[$i][4] == 0){ return $l; }
level($ff[$i][4], ++$l);
}
}
now there's even less to step in. ;^)
Re: problem returning value using recursive function
alright, so i've got some free-time. ;^)
i'm guessing your function recurses to 1) find that a directory exists and
2) find out how deeply nested it is. based on that, this should work just
fine:
<?
function combinePath(&$directory, $index, $path)
{
$directory = $path . '/' . $directory;
}
function getDirectories($path, &$tree = array())
{
$tree[$path] = $path;
$directory = opendir($path);
while (($fileName = readdir($directory)) !== false)
{
if ($fileName == '.' || $fileName == '..'){ continue; }
if (!is_dir($fileName)){ continue; }
$directories[] = $fileName;
}
closedir($directory);
if (!$directories){ return $tree; }
array_walk($directories, 'combinePath', $path);
foreach ($directories as $directory)
{
getDirectories($directory, $tree);
}
return $tree;
}
$path = 'c:/inetpub/wwwroot/someserver';
$testPath = 'c:/inetpub/wwwroot/someserver/css';
$directories = getDirectories($path);
$level = 0;
if (in_array($testPath, $directories))
{
$testPath = substr($testPath, strlen($path));
$level = strlen($testPath) - strlen(str_replace('/', '', $testPath));
}
echo '<pre>' . $level . '</pre>';
?>
you may now turn this to shit at will. :)
PHP » alt.php » problem returning value using recursive function