Dynamic variable names: Difference between revisions
Content added Content deleted
(Lingo added) |
(Added FreeBASIC) |
||
Line 203: | Line 203: | ||
pad swap 9 + evaluate</lang> |
pad swap 9 + evaluate</lang> |
||
Of course, it is easier for the user to simply type VARIABLE ''name'' at the Forth console. |
Of course, it is easier for the user to simply type VARIABLE ''name'' at the Forth console. |
||
=={{header|FreeBASIC}}== |
|||
FreeBASIC is a statically typed, compiled language and so it is not possible to create new variables, dynamically, at run time. However, you can make it look to the user like you are doing so with code such as the following. Ideally, a 'map' should be used for an exercise such as this but, as there isn't one built into FB, I've used a dynamic array instead which is searched linearly for the variable name. |
|||
<lang freebasic>' FB 1.05.0 Win64 |
|||
Type DynamicVariable |
|||
As String name |
|||
As String value |
|||
End Type |
|||
Function FindVariableIndex(a() as DynamicVariable, v as String, nElements As Integer) As Integer |
|||
v = LCase(Trim(v)) |
|||
For i As Integer = 1 To nElements |
|||
If a(i).name = v Then Return i |
|||
Next |
|||
Return 0 |
|||
End Function |
|||
Dim As Integer n, index |
|||
Dim As String v |
|||
Cls |
|||
Do |
|||
Input "How many variables do you want to create (max 5) "; n |
|||
Loop Until n > 0 AndAlso n < 6 |
|||
Dim a(1 To n) As DynamicVariable |
|||
Print |
|||
Print "OK, enter the variable names and their values, below" |
|||
For i As Integer = 1 to n |
|||
Print |
|||
Print " Variable"; i |
|||
Input " Name : ", a(i).name |
|||
a(i).name = LCase(Trim(a(i).name)) ' variable names are not case sensitive in FB |
|||
If i > 0 Then |
|||
index = FindVariableIndex(a(), a(i).name, i - 1) |
|||
If index > 0 Then |
|||
Print " Sorry, you've already created a variable of that name, try again" |
|||
i -= 1 |
|||
Continue For |
|||
End If |
|||
End If |
|||
Input " Value : ", a(i).value |
|||
a(i).value = LCase(Trim(a(i).value)) |
|||
Next |
|||
Print |
|||
Print "Press q to quit" |
|||
Do |
|||
Print |
|||
Input "Which variable do you want to inspect "; v |
|||
If v = "q" OrElse v = "Q" Then Exit Do |
|||
index = FindVariableIndex(a(), v, n) |
|||
If index = 0 Then |
|||
Print "Sorry there's no variable of that name, try again" |
|||
Else |
|||
Print "It's value is "; a(index).value |
|||
End If |
|||
Loop |
|||
End</lang> |
|||
Sample input/output : |
|||
{{out}} |
|||
<pre> |
|||
How many variables do you want to create (max 5) ? 3 |
|||
OK, enter the variable names and their values, below |
|||
Variable 1 |
|||
Name : a |
|||
Value : 1 |
|||
Variable 2 |
|||
Name : b |
|||
Value : 2 |
|||
Variable 3 |
|||
Name : b |
|||
Sorry, you've already created a variable of that name, try again |
|||
Variable 3 |
|||
Name : c |
|||
Value : 4 |
|||
Press q to quit |
|||
Which variable do you want to inspect ? b |
|||
It's value is 2 |
|||
Which variable do you want to inspect ? c |
|||
It's value is 4 |
|||
Which variable do you want to inspect ? a |
|||
It's value is 1 |
|||
Which variable do you want to inspect ? q |
|||
</pre> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |