Using Alpha Five's Eval() and Eval_Valid() Functions

by Dr. Peter Wayne

There are several undocumented functions that were added to Alpha Five version 3 after the manuals were printed. Two of the most useful are the eval() and eval_valid() functions. Sometimes they are exactly what you need.

Alpha Five has 2 functions that attempt to evaluate a string as an Alpha Five expression. One function, the eval_valid() function, just checks to see if the string produces a legitimate Xbasic or Alpha Five expression. The other, the eval() function, evaluates the string as a complete Alpha Five expression. I'll give some examples of how to use these functions.

eval_valid()

We'll take the eval_valid() function first, because it's conceptually simpler. A simple Xbasic expression is 2*5. We can test if this is a valid expression by writing, in the interactive script editor,

? eval_valid("2*5")
.T.

Well, that wasn't very exciting. What if we try these lines:

a=5
? eval_valid("2*a")
= .T.

? eval_valid("2a")
= .F.

Here we see that, if the variable a is defined, then 2*a is a valid Alpha Five expression, but 2a is not.

So what, I can hear you saying. All right, let's do something useful with eval_valid(). You know that Alpha Five has a number of type-checking functions, the isXXX functions:

You've probably used these functions in field rules and operations. Maybe you've felt the need for another function, isDate, which can verify whether a given string is a valid date. Let's create this function. If you've never created a global function, click on the Code tab of the Control Panel and select New, Function. Then indicate that the function returns a logical result and takes a single argument of type character as input:

Create New Global Function

Fig. 1. Create New Global Function

The actual code for the function is simplicity itself:isDate function
Fig. 2. Code for the isDate() function.

Here's how the isDate() function can be used in the Interactive window:

? isDate("2/29/96")
= .T.

? isDate("2/29/98")
= .F.

? isDate("1/32/98")
= .F.

? isDate("1/31/98")
= .T.

? isDate("2/29/2000")
= .T.

You can see that isDate() returns .T. for strings that can become valid dates, .F. for strings that would produce invalid dates. This new global function can be used in field rules and scripts just like any of Alpha Five's built-in functions, and even called by other functions.

eval()

The eval() function is similar to eval_valid(): it takes a string and attempts to interpret it as an Alpha Five expression. Unlike eval_valid(), which only returns .T. or .F., eval() returns the output of the expression. An illustration will be helpful:

dim day as n
dim year as c
dim month as c
dim MyDate as c

month="2"
day="29"
year="2000"
MyDate=eval("{"+month+"/"+day+"/"+year+"}")
? MyDate
= {02/29/2000}

Here, I used the eval() function as a substitute for the ctod() function. Big deal, I hear you muttering. Here's an example in which we set the value of a series of array elements to the values of simple variables:

dim MyArray[5] as n
dim A1 as n
dim A2 as n
dim A3 as n
dim A4 as n
dim A5 as n
A1=1
A2=2
A3=3
A4=4
A5=5
for i=1 to 5; MyArray[i]=eval("A"+ltrim(str(i))); next
? MyArray[4]
= 4.000000

In this example, the eval() function was used in a for-next loop to substitute for what would otherwise be 5 separate assignment statements.

We can come up with a more useful example. Suppose we have a form with a number of array variables, say, A[1] through A[20]. And we want to fill the first 20 fields of a table with the values in these variables. We can simplify the coding considerably by writing it like this:

''XBasic
t=table.open("MyTable")
dim shared A[20] as n

' **********************************************
' these next 3 lines needed only for this example
' in a real application, the array in A[i] would be filled
' on the form or in another script
for i=1 to 20
	A[i]=i 
next i
' ***********************************************

t.enter_begin()
for i=1 to 20 
	fd=t.field_get(i)
	evalstr="fd.value_put("+ltrim(str(A[i]))+")"
	eval(evalstr)
next i
t.enter_end(.t.)
t.close()

In this case, we sequentially evaluate an expression of the form fd.value_put(A[1]), and fill the fields of the table through another for-next loop.

In this short article, we have discussed using 2 undocumented but useful functions in Alpha Five. The only reason these functions are undocumented is that they were added after the Professional Reference manual was printed. They were added because there are some tasks that are best accomplished using these functions. Add them to your suitcase of Alpha Five tricks - that's why they are there!

9/8/98 - pkw

Don't forget, we need your feedback to make this site better!

Return to home