Page 1 of 1

Global variables are not passed to sub functions

Posted: 10 Dec 2016 23:19
by bjarne.dein
Hi,

I have this script:
----------------------------------

Code: Select all

$srcFolder  = <curpath>;
	global $folderPattern;
	global $logFile;
	$folderPattern = "([1|2]\d{3})\\([0|1]\d{1})\\([1|2]\d{3}_[0|1]\d{1}_[0-3]\d{1})($| - \w.*)";  //C|D|a|b
	$logFile = "%appdata%\XYplorer\Log\SyncDistination_<date yyyy-mm-dd_hh.nn.ss>.log";
	//echo "xydate : <xydata> xypath : <xypath>  xycatalogs : <xycatalogs> appdata: %appdata%\XYplorer\Log curfile: <curfile>";
	$dstFolders = get("path", "i");
	$srcLevel = 0;
	foreach($folder, $srcFolder, "\", "r"){
		$nLevels++;
		switch (folderLevel($folder)) {
			case 1: $srcFldLevel1 = $folder; if ($srcLevel = 0) { $srcLevel = 1;} break;
			case 2: $srcFldLevel2 = $folder; if ($srcLevel = 0) { $srcLevel = 2;} break;
			case 3: $srcFldLevel3 = $folder; if ($srcLevel = 0) { $srcLevel = 3;} break;
			case 4: $srcFldLevel4 = $folder; if ($srcLevel = 0) { $srcLevel = 4;} break;
		}
		if($nLevels = 4) { break; }
	}
	
	switch($srcLevel){
		case 1: break;
		case 2: break;
		case 3: break;
		case 4: break;
	}

	function folderLevel($folder){
		foreach ($levelPattern, $folderPattern, "\\") {
			echo "$levelPattern";
			$nLevel++;
			$match = regexmatches($folder,$levelPattern);
			if(strlen($match) > 0) { return $nLevel; }
		}
		return 0;
	}
----------------------------------
where "global $folderPattern" is not visible to the function: "folderLevel", why is that?

PS. I'm currently on version 17.40

BR Bjarne

Re: Global variables are not passed to sub functions

Posted: 11 Dec 2016 00:09
by PeterH
(You should use a codebox to show code.)

Global is only defined for the current scope, so these variables must also be defined as Global inside the function.

If you would use Perm this would not be neccessary: Perm is used for all scopes.

(See description of Global and Perm commands.)

Re: Global variables are not passed to sub functions

Posted: 11 Dec 2016 10:45
by highend
I've added the bb code to your posting.

While looking over your code:
You are not checking for variable content but you're assigning a value to a variable in some cases

E.g.:

Code: Select all

if ($srcLevel = 0)
or

Code: Select all

if($nLevels = 4) { break; }
These will always be true because you're not using the comparison operator "==" here regardless of what $srcLevel or $nLevels contain!

Re: Global variables are not passed to sub functions

Posted: 12 Dec 2016 00:34
by bjarne.dein
highend wrote:I've added the bb code to your posting.

While looking over your code:
You are not checking for variable content but you're assigning a value to a variable in some cases
Bjarne wrote:I'm aware of this, but thanks anyway
E.g.:

Code: Select all

if ($srcLevel = 0)
or

Code: Select all

if($nLevels = 4) { break; }
These will always be true because you're not using the comparison operator "==" here regardless of what $srcLevel or $nLevels contain!

Re: Global variables are not passed to sub functions

Posted: 12 Dec 2016 00:35
by bjarne.dein
Thanks for the hints:-)