Global variables are not passed to sub functions

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
bjarne.dein
Posts: 14
Joined: 16 Nov 2016 23:05
Location: Havdrup, Denmark

Global variables are not passed to sub functions

Post 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

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Global variables are not passed to sub functions

Post 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.)

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Global variables are not passed to sub functions

Post 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!
One of my scripts helped you out? Please donate via Paypal

bjarne.dein
Posts: 14
Joined: 16 Nov 2016 23:05
Location: Havdrup, Denmark

Re: Global variables are not passed to sub functions

Post 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!

bjarne.dein
Posts: 14
Joined: 16 Nov 2016 23:05
Location: Havdrup, Denmark

Re: Global variables are not passed to sub functions

Post by bjarne.dein »

Thanks for the hints:-)

Post Reply