Originally published September 19, 2017 @ 8:56 pm

Let’s say you have a dozen variables in your script and you need to check if they have values set. The usual approach can get a bit tedious.

This gets even worse if you need to assign some default value to any variable that is not set:

Here’s a different approach that can save you a lot of typing:

If you really have lots and lots of variables that require different default values, you may use a table. Parse this table to populate two arrays: one containing names of the variables and the other – their default values. Let’s say your table.txt looks like this:

We will use it to build two arrays:

Now we can read the array_variables array, check if the variables are set and, if not, assign a corresponding default value from the array_defaults array:

And to check that the variables have been set: