Jump to content

Global Variables use in Custom Functions - Just out of Curiousity


This topic is 4594 days old. Please don't post here. Open a new topic instead.

Recommended Posts

You can refer to global variables in a custom function, and you can also define them via a Let function.

I would be sure to include a check for IsEmpty( $$yourGlobalVar ) and also keep in mind the usual general cautions about usage of global variables.

Did you have a specific question?

Link to comment
Share on other sites

  • 1 month later...

I'm a huge fan of both Global and local variables in custom functions. There's heavy use of both in my UUID custom functions for passing data between recursive calls and for saving data between calls: https://github.com/j...UIDTimeNIC.fmfn

The trick is always remembering to clean-up any variables I use when I'm done. I also usually wind up having to implement these types of custom functions as scripts first so I can work out the details with the debugger rather than trying to clutter a calculation with development exceptions for tracing what's going on.

Link to comment
Share on other sites

  • 3 months later...

I'm a huge fan of both Global and local variables in custom functions. There's heavy use of both in my UUID custom functions for passing data between recursive calls and for saving data between calls: https://github.com/j...UIDTimeNIC.fmfn

The trick is always remembering to clean-up any variables I use when I'm done. I also usually wind up having to implement these types of custom functions as scripts first so I can work out the details with the debugger rather than trying to clutter a calculation with development exceptions for tracing what's going on.

Jeremy ; can you elaborate on how you do that? Or do just write the scripts and then convert them to the custom function.? this is definately something i am running into as it takes forever for me to test what is going on line by line.

thanks

Link to comment
Share on other sites

Jeremy ; can you elaborate on how you do that? Or do just write the scripts and then convert them to the custom function.? this is definately something i am running into as it takes forever for me to test what is going on line by line.

thanks

Imoree, if I know that a custom function will be particularly tricky — something with several sequential steps or any looping process — I'll often prototype the functionality in a script first. That way, I can walk through step-by-step and see what values are in which variables every step of the way with the data viewer. Debugging a script is much easier than debugging an equivalent custom function, since you can't inspect intermediate states while a custom function is running. Once I have the algorithm working right in script form, I then reproduce it as faithfully as possible as a custom function.

  • Like 1
Link to comment
Share on other sites

Imoree, if I know that a custom function will be particularly tricky — something with several sequential steps or any looping process — I'll often prototype the functionality in a script first. That way, I can walk through step-by-step and see what values are in which variables every step of the way with the data viewer. Debugging a script is much easier than debugging an equivalent custom function, since you can't inspect intermediate states while a custom function is running. Once I have the algorithm working right in script form, I then reproduce it as faithfully as possible as a custom function.

appreciate the repsonse jeremy..

Link to comment
Share on other sites

So in response again Jeremy; How would one go about putting this into a script to test for functionality and variable values?


Case (

	/*Pull node field from UUID, and set-up conversion loop*/

	not $~step;

		Let ( [

			~idAsNumber = GetAsNumber ( theID );

			~version = Left ( ~idAsNumber ; Length ( ~idAsNumber ) - 40 );

			$~nicAddress = Right ( ~idAsNumber ; 15 );

			~isMulticast = Mod ( Div ( $~nicAddress ; 1099511627776 ) ; 2 );

			$~hexNICAddress = "";

			$~base16 = "0123456789abcdef";

			$~step =

				If (

					~version < 3 //NIC is valid

					and $~nicAddress &#8804; 281474976710656

					and not ~isMulticast;

						1;

					/*else*/

						-1 //error

				)

		];

			UUIDGetNICAddress ( "" )

		);



	/*Convert NIC address from base 10 to base 16*/

	$~step = 1;

		Let ( [

			$~hexNICAddress =

				Middle ( $~base16 ; Mod ( $~nicAddress ; 16 ) + 1 ; 1 ) &

				$~hexNICAddress;

				$~nicAddress = Div ( $~nicAddress ; 16 );

				$~step = If ( $~nicAddress ; $~step ; /*else*/ $~step + 1 )

		];

			UUIDGetNICAddress ( "" )

		);



	/*Insert colons and return result*/

	$~step = 2;

		Let ( [

			~nicAddress = Right ( "000000000000" & $~hexNICAddress ; 12 );

			~nicAddress =

				Middle ( ~nicAddress ; 1 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 3 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 5 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 7 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 9 ; 2 ) & ":"

				& Middle ( ~nicAddress ; 11 ; 2 );



			//purge variables

			$~nicAddress = "";

			$~hexNICAddress = "";

			$~base16 = "";

			$~i = "";

			$~step = ""

		];

			~nicAddress

		);



	/*Return error value*/

	$~step = -1;

		Let ( [

			//purge variables

			$~nicAddress = "";

			$~hexNICAddress = "";

			$~base16 = "";

			$~i = "";

			$~step = ""

		];

			"" //Null

		)

)

 

i am sorry , but i understand coding in an ide from many years ago. NOw you are teaching me something very new, which i would love to learn.

-ian

Os it just a bunch of "set Variable" statements . loops & calculations?

Link to comment
Share on other sites

It's mostly Set Variable [] statements. The Case() becomes an If[]/Else If[] series, the Let()s become a series of Set Variable[], the $variables become $$variables, the ~letVariables become $variables, and the recursive custom function calls become recursive script calls followed by something using Get ( ScriptResult ) — Exit Script [Result: Get ( ScriptResult )] in the case of a tail recursive function.

Link to comment
Share on other sites

This topic is 4594 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.