Difference between revisions of "Line number"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 95: Line 95:
 
* [[GOTO]]
 
* [[GOTO]]
 
* [[GOSUB]]
 
* [[GOSUB]]
 
+
* [[RESUME]]
 +
* [[RUN]]
 +
* [[IF...THEN]]
  
 
{{PageNavigation}}
 
{{PageNavigation}}

Revision as of 15:55, 5 March 2012

Line numbers or line labels are used in Qbasic as references to code lines. Numbers are required with GOTO and numbers or label names are required with GOSUB. Although not required in Qbasic, other older programming languages such as GW Basic often required them. Line labels require a colon after them to differentiate them from keywords and variable names.


The following program can remove line numbers from code where they are not required by GOTO or GOSUB:

CLS REDIM linenumber(5000) PRINT "This Program removes unnecessary line numbers.": PRINT INPUT "Name of file: ", filename$: PRINT OPEN filename$ FOR INPUT AS #1 DO UNTIL EOF(1) LINE INPUT #1, a$ REM Get rid of quotes and REM statements. DO IF INSTR(a$, CHR$(34)) <> 0 THEN IF INSTR(INSTR(a$, CHR$(34)) + 1, a$, CHR$(34)) = 0 THEN EXIT DO: REM Mismatched quotes. Illegal code. a$ = MID$(a$, 1, INSTR(a$, CHR$(34)) - 1) + MID$(a$, INSTR(INSTR(a$, CHR$(34)) + 1, a$, CHR$(34)) + 1) ELSE EXIT DO END IF LOOP IF INSTR(UCASE$(a$), "REM ") <> 0 THEN a$ = MID$(a$, 1, INSTR(UCASE$(a$), "REM ")) IF INSTR(UCASE$(a$), "' ") <> 0 THEN a$ = MID$(a$, 1, INSTR(a$, "' ")) DO flag = 0 REM Break down compound statements and get line numbers. IF INSTR(a$, "GOTO ") <> 0 THEN flag = 1: n = INSTR(a$, "GOTO ") + 5: GOSUB tracknumber IF INSTR(a$, "GOSUB ") <> 0 THEN flag = 1: n = INSTR(a$, "GOSUB ") + 6: GOSUB tracknumber IF INSTR(a$, "THEN ") <> 0 THEN flag = 1: n = INSTR(a$, "THEN ") + 5: GOSUB tracknumber IF INSTR(a$, "ELSE ") <> 0 THEN flag = 1: n = INSTR(a$, "ELSE ") + 5: GOSUB tracknumber IF INSTR(a$, "ERL ") <> 0 THEN flag = 1: n = INSTR(a$, "ELSE ") + 4: GOSUB tracknumber IF flag = 0 THEN EXIT DO ELSE a$ = MID$(a$, n) LOOP LOOP PRINT "Number of line-numbered statements kept:"; count CLOSE #1 OPEN filename$ FOR INPUT AS #1 OPEN "filename.tmp" FOR OUTPUT AS #2 DO UNTIL EOF(1) LINE INPUT #1, a$ flag = 0 IF VAL(MID$(a$, 1, 1)) <> 0 THEN FOR i = 1 TO count IF VAL(a$) = linenumber(i) THEN flag = 1: EXIT FOR NEXT IF flag = 0 THEN a$ = MID$(a$, INSTR(a$, " ") + 1) END IF PRINT #2, a$ LOOP CLOSE #1, #2 SHELL "cmd/c start notepad filename.tmp" PRINT PRINT "Name the line edited file as the original file and backup original file? [Y/N]" DO b$ = UCASE$(INKEY$) IF b$ = "N" OR b$ = CHR$(27) THEN EXIT DO IF b$ = "Y" THEN ON ERROR GOTO resnxt KILL MID$(filename$, 1, INSTR(filename$, ".") - 1) + ".org" ON ERROR GOTO 0 NAME filename$ AS MID$(filename$, 1, INSTR(filename$, ".") - 1) + ".org" NAME "filename.tmp" AS filename$ PRINT : PRINT "File conversion completed." EXIT DO END IF LOOP SYSTEM tracknumber: DO count = count + 1 linenumber(count) = VAL(MID$(a$, n)) IF INSTR(a$, "ON") <> 0 AND INSTR(a$, "GOSUB") <> 0 OR INSTR(a$, "ON") <> 0 AND INSTR(a$, "GOTO") <> 0 THEN FOR i = n TO LEN(a$) IF VAL(MID$(a$, i, 1)) = 0 AND MID$(a$, i, 1) <> "0" AND MID$(a$, i, 1) <> "," AND MID$(a$, i, 1) <> " " THEN EXIT FOR IF MID$(a$, i, 2) = ", " THEN n = i + 1: EXIT FOR NEXT IF n <> i + 1 THEN EXIT DO ELSE EXIT DO END IF LOOP RETURN resnxt: RESUME NEXT

Code courtesy of Pete from the N54 Qbasic Forum


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page