This article is part of my series Custom Function Vault. Here I describe a custom function for working with parameters.
param.assign( params )
From the Beginning
In my previous entry I described two customs functions for managing parameters: param and param.get. They encode and decode named (script) parameters. I started using them a lot and began thinking about further improvements. Some ideas were useful and made it into my standard set of custom functions. Other ideas were just crazy or not really viable, but just playing with them helped to create new ideas and during the process I learned more about FileMaker.
At that time, a friend told me about similar solutions from other developers. He showed me Matt Petrowsky’s video Function Scripting at ISO FileMaker Magazine and Mikhail Edoshin’s website and his solution to decode parameters. Matt also credits Alexander Zuiev for a custom function that takes the name of the script and the script parameters and assigns them automatically to variables. I liked that idea very much. My script names already include the name of the expected variable names. I begun developing my own solution for this problem.
Developing the Idea
The idea is, developing a solution that automatically assigns script parameters to local variables and checks, whether all required parameters were passed to the script. Naturally, the solution divides into two parts:
- Assigning parameters to variables
- Check existence of required script parameters
The first part is easy, actual processed with a single custom function that I will describe here. The second part requires multiple steps (evaluating parameter names, verifying their existance). It requires multiple custom functions and I will write about it in a later blog entry.
CF param.assign( params )
The custom function expects one value, a list a parameters. It returns the number of assigned local variables. The function could return NULL (empty string), but sometimes it might be valuable to know the number of assigned variables.
Let( [ _pos = Position( params; "=="; 1; 1 ); _name = Left( params; _pos - 1 ); _value = If( IsEmpty( _name ); ""; param.get( _name; params ) ) ]; If( not IsEmpty( _name ) and not IsEmpty( _value ); Evaluate( "Let($" & _name & "=" & Quote( _value ) & "; 1 )" ) + param.assign( MiddleValues( params; 2; 1000000 ) ); // Else 0 ) )
What is it doing?
It is searching the first appearance of two equal signs, extracting all text before them, using it as the name of the parameter. With the name of the parameter, it retrieves the parameter value, using the function param.get.
The function uses the parameter name and value to create an expression that can be evaluated. The expression will assign the parameter value to a local variable with the same name as the parameter name. The evaluated expression will also returning the value 1, adding it to the total number of processed parameters.
Finally, the custom function is call recursively with the remaining parameters to be processed.
You may wonder, why I do not substitute special characters when I retrieve the parameter name, as I am doing in the function param.get. Actual, it does not make a lot of sense to work with a parameter name that contains a carriage return or a paragraph sign. You can not create Filemaker variables with these characters included. Of course, you could modify the custom function, replacing these special characters with underscores (_) or any other character. I choose to ignore them. The evaluated expression will not create the variable and it returns 0, adding the right number to the amount of assigned parameters.
param.assign( "firstname==Arnold¶lastname==Kegebein¶city==Hamburg"; "lastname" )
It also creates three local variables:
- $firstname = “Arnold”
- $lastname = “Kegebein”
- $city = “Hamburg”
Using it in Scripts
You cannot call a function by itself in a script. You have to assign it to a variable
Set Variable [$count; Value: param.assign( $params )]
or use it within an expression
If [param.assign( $params ) > 0] ... Else ... End If
Here you can check whether any variables were assigned.
More to come
This was only the first part of it. My next article will explain, how I check the created variables against the variable required for the current script. It will introduce a couple of new custom functions, useful for many situations.