Secondly, in the last article I made an error. I said to put WAITFORKEY in the script.dat file but in the script.bas file we called it WAITKEY. Change WAITFORKEY in script.dat to WAITKEY.
Third, in this issue I'm gonna talk about variables and how to use them. You need the script.bas program and the script.dat file we created in issue #1 to use this article, if you don't have it I suggest you read the article in issue #1.
Variables
Unlike QBasic we're gonna have two types of variables, text and numeric. We're gonna modify our DISPLAYTEXT command so that it can handle variables and equations. First you need to add this two lines at the beginning of your script.bas file.
DIM SHARED Variables$(100,2) DIM SHARED VariableCount%What we've done is that we've made one arrays which hold the variable name, type and the value. VariableCount% controls how many variables we allready have in memory. Add this at the beginning of our script.dat file:
$text = Hello! This is Nightwolf's Scripting language article!! %a = 5 %b = 10 DISPLAYTEXT $text DISPLAYTEXT (%a * %b)This creates one text variable ($text) with the value of 'Hello! This is Nightwolf's Scripting language article!!' and two numeric values (%a and %b) with the value of 5 and 10. Then it displays the value of $text and displays the equation %a times %b. First we're gonna add variable detection and storage. Add this at the beginning of the DO...LOOP command just behind the reading of the line (just after
INPUT #1, ReadLine$
):
IF LEFT$(ReadLine$, 1) = "$" THEN IF VariableCount% <= 100 THEN VariableCount% = VariableCount% + 1 Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1) Variables$(VariableCount%, 1) = "1" Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1) END IF END IF IF LEFT$(ReadLine$, 1) = "%" THEN IF VariableCount% <= 100 THEN VariableCount% = VariableCount% + 1 Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1) Variables$(VariableCount%, 1) = "2" Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1) END IF END IFI'm gonna explain the code line by line now.
IF LEFT$(ReadLine$, 1) = "$" THENIf the first character of ReadLine$ is a $ then it's a text variable and we start the IF.
IF VariableCount% <= 100 THENIf we've less than 100 variables in memory we continue.
VariableCount% = VariableCount% + 1Increases the number of variables in memory by 1.
Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1)Puts the name of the variable in Variables$. (name = all characters to the first space)
Variables$(VariableCount%, 1) = "1"Set the type of variable to 1 (text).
Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1)Set the value of the variable to all the characters behind the = and the space following the =.
END IFEnd the IF
END IFEnd the IF
IF LEFT$(ReadLine$, 1) = "%" THENIf the first character of ReadLine$ is a % then it's a numeric variable and we start the IF.
IF VariableCount% <= 100 THEN
VariableCount% = VariableCount% + 1If we've less than 100 variables in memory we continue.
Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1)Puts the name of the variable in Variables$. (name = all characters to the first space)
Variables$(VariableCount%, 1) = "2"Set the variable type to 2 (numeric).
Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1)Set the value of the variable to all the characters behind the = and the space following the =.
END IFEnd the IF.
END IFEnd the IF.
Displaying variables and equations
IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THEN Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12) PRINT Temp1$ END IFAnd then replace it with this:
IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THEN Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12) IF LEFT$(Temp1$, 1) = "$" OR LEFT$(Temp1$, 1) = "%" OR LEFT$(Temp1$, 1) = "(" THEN IF LEFT$(Temp1$, 1) = "(" THEN SELECT CASE MID$(Temp1$, INSTR(Temp1$, " ") + 1, 1) CASE "+" Action% = 1 CASE "-" Action% = 2 CASE "*" Action% = 3 CASE "/" Action% = 4 END SELECT Temp2$ = MID$(Temp1$, 2, INSTR(Temp1$, " ") - 1) Temp3$ = RIGHT$(Temp1$, LEN(Temp1$) - INSTR(INSTR(Temp1$, " ") + 1, Temp1$, " ")) Temp3$ = LEFT$(Temp3$, LEN(Temp3$) - 1) Temp2$ = LTRIM$(RTRIM$(Temp2$)) Temp3$ = LTRIM$(RTRIM$(Temp3$)) FOR i = 1 TO VariableCount% IF Variables$(i, 0) = Temp2$ THEN Temp1% = VAL(Variables$(i, 2)) IF Variables$(i, 0) = Temp3$ THEN Temp2% = VAL(Variables$(i, 2)) NEXT i SELECT CASE Action% CASE 1 PRINT Temp1% + Temp2% CASE 2 PRINT Temp1% - Temp2% CASE 3 PRINT Temp1% * Temp2% CASE 4 PRINT Temp1% / Temp2% END SELECT ELSE FOR i = 1 TO VariableCount% IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "1" THEN PRINT Variables$(i, 2) IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "2" THEN PRINT VAL(Variables$(i, 2)) NEXT i END IF ELSE PRINT Temp1$ END IF END IFI'll explain the code line by line now:
IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THENIf the first 11 characters of the ReadLine$ variable are DISPLAYTEXT execute the DISPLAYTEXT command.
Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12)This gets the text, variable or equation to display from the ReadLine$ variable and stores it in Temp1$.
IF LEFT$(Temp1$, 1) = "$" OR LEFT$(Temp1$, 1) = "%" OR LEFT$(Temp1$, 1) = "(" THENIf the first character of Temp1$ is a $, % or ( then we start the variable/equation IF.
IF LEFT$(Temp1$, 1) = "(" THENIf the first character of Temp1$ is a (, start the equation IF.
SELECT CASE MID$(Temp1$, INSTR(Temp1$, " ") + 1, 1)Start a SELECT CASE command for the first character behind the first space in Temp1$. This is the equation method (+, -, *, /).
CASE "+" Action% = 1If it's a + (plus) then set Action% to 1.
CASE "-" Action% = 2If it's a - (minus) then set Action% to 2.
CASE "*" Action% = 3If it's a * (times) then set Action% to 3.
CASE "/" Action% = 4If it's a / (divide by) then set Action% to 4.
END SELECTEnd the SELECT CASE command.
Temp2$ = MID$(Temp1$, 2, INSTR(Temp1$, " ") - 1)The first variable name.
Temp3$ = RIGHT$(Temp1$, LEN(Temp1$) - INSTR(INSTR(Temp1$, " ") + 1, Temp1$, " "))Get the second variable name.
Temp3$ = LEFT$(Temp3$, LEN(Temp3$) - 1)Remove the last character from the second variable name because when we got it in the last step there still was the ) of an equation
Temp2$ = LTRIM$(RTRIM$(Temp2$))Remove all the spaces left and right from the first variable name.
Temp3$ = LTRIM$(RTRIM$(Temp3$))Remove all the spaces left and right from the second variable name.
FOR i = 1 TO VariableCount%Start a FOR...NEXT from 1 to the number of varialbes we have in memory.
IF Variables$(i, 0) = Temp2$ THEN Temp1% = VAL(Variables$(i, 2))If the variable names are the same make Temp1% equal to the value of the variable.
IF Variables$(i, 0) = Temp3$ THEN Temp2% = VAL(Variables$(i, 2))If the variable names are the same make Temp2% equal to the value of the variable.
NEXT iReturn to the FOR
SELECT CASE Action%Start a SELECT CASE of the value of Action%
CASE 1 PRINT Temp1% + Temp2%If Action% is a 1 (plus) then do the equation.
CASE 2 PRINT Temp1% - Temp2%If Action% is a 2 (minus) then do the equation.
CASE 3 PRINT Temp1% * Temp2%If Action% is a 3 (times) then do the equation.
CASE 4 PRINT Temp1% / Temp2%If Action% is a 4 (divide) then do the equation.
END SELECTEnd the SELECT CASE.
ELSEIf it's not an equation then:
FOR i = 1 TO VariableCount%Start a FOR...NEXT loop from 1 to the number of variables we have in memory.
IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "1" THEN PRINT Variables$(i, 2)If the variable names are the same and the type is 1 (text) print the value of that variable.
IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "2" THEN PRINT VAL(Variables$(i, 2))If the variable names are the same and the type is 2 (numeric) print the numeric value of that variable.
NEXT iGo back to the FOR
END IFEnd the if
ELSEIf it's not an equation or variable display:
PRINT Temp1$Just print the expression that was behind the DISPLAYTEXT command
END IFEnd the If
END IFEnd the If
Conclusion
DIM SHARED Variables$(100, 3) DIM SHARED VariableCount% OPEN "script.dat" FOR INPUT AS #1 DO INPUT #1, ReadLine$ IF LEFT$(ReadLine$, 1) = "$" THEN IF VariableCount% <= 100 THEN VariableCount% = VariableCount% + 1 Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1) Variables$(VariableCount%, 1) = "1" Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1) END IF END IF IF LEFT$(ReadLine$, 1) = "%" THEN IF VariableCount% <= 100 THEN VariableCount% = VariableCount% + 1 Variables$(VariableCount%, 0) = LEFT$(ReadLine$, INSTR(ReadLine$, " ") - 1) Variables$(VariableCount%, 1) = "2" Variables$(VariableCount%, 2) = RIGHT$(ReadLine$, LEN(ReadLine$) - INSTR(ReadLine$, "=") - 1) END IF END IF ReadLine$ = LTRIM$(RTRIM$(ReadLine$)) IF LEFT$(ReadLine$, 11) = "DISPLAYTEXT" THEN Temp1$ = RIGHT$(ReadLine$, LEN(ReadLine$) - 12) IF LEFT$(Temp1$, 1) = "$" OR LEFT$(Temp1$, 1) = "%" OR LEFT$(Temp1$, 1) = "(" THEN IF LEFT$(Temp1$, 1) = "(" THEN SELECT CASE MID$(Temp1$, INSTR(Temp1$, " ") + 1, 1) CASE "+" Action% = 1 CASE "-" Action% = 2 CASE "*" Action% = 3 CASE "/" Action% = 4 END SELECT Temp2$ = MID$(Temp1$, 2, INSTR(Temp1$, " ") - 1) Temp3$ = RIGHT$(Temp1$, LEN(Temp1$) - INSTR(INSTR(Temp1$, " ") + 1, Temp1$, " ")) Temp3$ = LEFT$(Temp3$, LEN(Temp3$) - 1) Temp2$ = LTRIM$(RTRIM$(Temp2$)) Temp3$ = LTRIM$(RTRIM$(Temp3$)) FOR i = 1 TO VariableCount% IF Variables$(i, 0) = Temp2$ THEN Temp1% = VAL(Variables$(i, 2)) IF Variables$(i, 0) = Temp3$ THEN Temp2% = VAL(Variables$(i, 2)) NEXT i SELECT CASE Action% CASE 1 PRINT Temp1% + Temp2% CASE 2 PRINT Temp1% - Temp2% CASE 3 PRINT Temp1% * Temp2% CASE 4 PRINT Temp1% / Temp2% END SELECT ELSE FOR i = 1 TO VariableCount% IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "1" THEN PRINT Variables$(i, 2) IF Variables$(i, 0) = Temp1$ AND Variables$(i, 1) = "2" THEN PRINT VAL(Variables$(i, 2)) NEXT i END IF ELSE PRINT Temp1$ END IF END IF IF LEFT$(ReadLine$, 7) = "WAITKEY" THEN WHILE INKEY$ = "": WEND IF LEFT$(ReadLine$, 9) = "ENDSCRIPT" THEN EXIT DO END IF LOOP UNTIL EOF(1) CLOSE #1
© Copyright 1999-2000 The QB Times.
This tutorial was taken from The QB Times with permission from the webmaster(s) of the site. You may not take this article and place it on any other website.