Difference between revisions of "Line number"

From QB64 Wiki
Jump to navigation Jump to search
m (Text replacement - "Qbasic" to "QBasic")
m (Add page sections/Reword)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''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.
+
'''Line identifiers''' are used to identify a specific line location in a program. There are two types of identifiers, line numbers and alphanumeric line labels.
  
 +
Line identifiers act as targets for statements that alter the normal flow of control of a program such as [[GOTO]] and [[GOSUB]].
  
<center>'''[ftp://ftp.microsoft.com/services/technet/windows/msdos/RESKIT/SUPPDISK/REMLINE.BAS Microsoft's REMLINE.BAS program removes unnecessary line numbers]'''</center>
 
  
 +
{{PageSyntax}}
 +
:: ''lineNumber'' [statement]
 +
:: ''lineLabel'': [statement]
  
The following program can remove line numbers where they are not required by [[IF...THEN]], [[GOTO]], [[GOSUB]], [[ERL]], [[RUN]] or [[RESUME]]:
+
 
 +
{{PageDescription}}
 +
* Line numbers are any combination of digits.
 +
* Line labels are any combination of letters and digits, starting with a letter, and followed by a colon.  The colon is required to differentiate them from keywords and variable names. Case is not significant for line labels.
 +
* Identifiers may start in any column but may only be proceeded by whitespace characters.
 +
* Line numbers and labels may be freely mixed in a given program.
 +
* The order of line numbers is not significant and there is no correspondence between line numbers and the actual line number, within the source code file, at which they appear.  Line numbers are simply numeric line labels.
 +
* Statements may immediately follow line identifiers. No intervening whitespace is required.
 +
* Line numbers are not required for every program line as they were in earlier dialects of the BASIC language.
 +
 
 +
 
 +
{{PageExamples}}
 +
''Example 1:''
 +
{{CodeStart}}
 +
100  {{Cl|PRINT}} "This is a line number."
 +
here: {{Cl|PRINT}} "This is a line label."
 +
{{CodeEnd}}
 +
 
 +
''Example 2:'' Microsoft's REMLINE.BAS program removes unnecessary line numbers where they are not required by [[IF...THEN]], [[GOTO]], [[GOSUB]], [[ERL]], [[RUN]] or [[RESUME]].
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
 
{{Cl|CLS}}
 
{{Cl|CLS}}
Line 14: Line 35:
 
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
 
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
 
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
 
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
{{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
+
    {{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
{{Cl|REM}} Get rid of quotes and {{Cl|REM}} statements.
+
    {{Cl|REM}} Get rid of quotes and REM statements.
DO
+
    {{Cl|DO}}
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) <> 0 {{Cl|THEN}}
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) <> 0 {{Cl|THEN}}
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) = 0 {{Cl|THEN}} {{Cl|EXIT DO}}: {{Cl|REM}} Mismatched quotes. Illegal code.
+
            {{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) = 0 {{Cl|THEN}} {{Cl|EXIT DO}}: {{Cl|REM}} Mismatched quotes. Illegal code.
a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) - 1) + {{Cl|MID$}}(a$, {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) + 1)
+
            a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) - 1) + {{Cl|MID$}}(a$, {{Cl|INSTR}}({{Cl|INSTR}}(a$, {{Cl|CHR$}}(34)) + 1, a$, {{Cl|CHR$}}(34)) + 1)
{{Cl|ELSE}}
+
        {{Cl|ELSE}}
{{Cl|EXIT DO}}
+
            {{Cl|EXIT DO}}
{{Cl|END IF}}
+
        {{Cl|END IF}}
{{Cl|LOOP}}
+
    {{Cl|LOOP}}
  
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} "))
+
    {{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "{{Cl|REM}} "))
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "' ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, "' "))
+
    {{Cl|IF...THEN|IF}} {{Cl|INSTR}}({{Cl|UCASE$}}(a$), "' ") <> 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, 1, {{Cl|INSTR}}(a$, "' "))
  
DO
+
    {{Cl|DO}}
flag = 0
+
        flag = 0
{{Cl|REM}} Break down compound statements and get line numbers.
+
        {{Cl|REM}} Break down compound statements and get line numbers.
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") + 5: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOTO}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") + 6: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|GOSUB}} ") + 6: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") + 5: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|THEN}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") + 5: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|ELSE}} ") + 5: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RUN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RUN}} ") + 4: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RUN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RUN}} ") + 4: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RESUME}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RESUME}} ") + 7: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RESUME}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RESUME}} ") + 7: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RETURN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RETURN}} ") + 7: {{Cl|GOSUB}} tracknumber
+
        {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "{{Cl|RETURN}} ") <> 0 {{Cl|THEN}} flag = 1: n = {{Cl|INSTR}}(a$, "{{Cl|RETURN}} ") + 7: {{Cl|GOSUB}} tracknumber
{{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} {{Cl|EXIT DO}} {{Cl|ELSE}} a$ = {{Cl|MID$}}(a$, n)
+
        {{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} {{Cl|EXIT DO}} {{Cl|ELSE}} a$ = {{Cl|MID$}}(a$, n)
{{Cl|LOOP}}
+
    {{Cl|LOOP}}
 
{{Cl|LOOP}}
 
{{Cl|LOOP}}
 
{{Cl|PRINT}} "Number of line-numbered statements kept:"; count
 
{{Cl|PRINT}} "Number of line-numbered statements kept:"; count
Line 45: Line 66:
  
 
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
 
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
{{Cl|OPEN}} "filename.tmp" {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #2
+
{{Cl|OPEN}} "filename.tmp" {{Cl|FOR (file statement)|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #2
 
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
 
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1)
{{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
+
    {{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, a$
flag = 0
+
    flag = 0
{{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, 1, 1)) <> 0 {{Cl|THEN}}
+
    {{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, 1, 1)) <> 0 {{Cl|THEN}}
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} count
+
        {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} count
{{Cl|IF...THEN|IF}} {{Cl|VAL}}(a$) = linenumber(i) {{Cl|THEN}} flag = 1: {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
+
            {{Cl|IF...THEN|IF}} {{Cl|VAL}}(a$) = linenumber(i) {{Cl|THEN}} flag = 1: {{Cl|EXIT FOR}}
{{Cl|NEXT}}
+
        {{Cl|NEXT}}
{{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, {{Cl|INSTR}}(a$, " ") + 1)
+
        {{Cl|IF...THEN|IF}} flag = 0 {{Cl|THEN}} a$ = {{Cl|MID$}}(a$, {{Cl|INSTR}}(a$, " ") + 1)
{{Cl|END IF}}
+
    {{Cl|END IF}}
{{Cl|PRINT (file statement)|PRINT}} #2, a$
+
    {{Cl|PRINT (file statement)|PRINT}} #2, a$
 
{{Cl|LOOP}}
 
{{Cl|LOOP}}
 
{{Cl|CLOSE}} #1, #2
 
{{Cl|CLOSE}} #1, #2
Line 61: Line 82:
 
{{Cl|PRINT}}
 
{{Cl|PRINT}}
 
{{Cl|PRINT}} "Back up original file as .org and give edited file original file name? [Y/N]"
 
{{Cl|PRINT}} "Back up original file as .org and give edited file original file name? [Y/N]"
DO
+
{{Cl|DO}}
b$ = {{Cl|UCASE$}}({{Cl|INKEY$}})
+
    b$ = {{Cl|UCASE$}}({{Cl|INKEY$}})
{{Cl|SELECT CASE}} b$
+
    {{Cl|SELECT CASE}} b$
{{Cl|CASE}} {{Cl|CHR$}}(27): {{Cl|SYSTEM}}
+
        {{Cl|CASE}} {{Cl|CHR$}}(27): {{Cl|SYSTEM}}
{{Cl|CASE}} "N"
+
        {{Cl|CASE}} "N"
{{Cl|ON ERROR}} {{Cl|GOTO}} RESNXT
+
            {{Cl|ON ERROR}} {{Cl|GOTO}} resnxt
{{Cl|KILL}} "filename.tmp"
+
            {{Cl|KILL}} "filename.tmp"
{{Cl|ON ERROR}} {{Cl|GOTO}} 0
+
            {{Cl|ON ERROR}} {{Cl|GOTO}} 0
{{Cl|PRINT}}
+
            {{Cl|PRINT}}
{{Cl|PRINT}} "Temporary "+ {{Cl|CHR$}}(34) + "filename.tmp" + {{Cl|CHR$}}(34) + " was removed and no changes were made to original file."
+
            {{Cl|PRINT}} "Temporary "+ {{Cl|CHR$}}(34) + "filename.tmp" + {{Cl|CHR$}}(34) + " was removed and no changes were made to original file."
{{Cl|PRINT}}
+
            {{Cl|PRINT}}
{{Cl|PRINT}} "Press any key to end."
+
            {{Cl|PRINT}} "Press any key to end."
{{Cl|EXIT DO}}
+
            {{Cl|EXIT DO}}
  
{{Cl|CASE}} "Y"
+
        {{Cl|CASE}} "Y"
{{Cl|ON ERROR}} {{Cl|GOTO}} resnxt
+
            {{Cl|ON ERROR}} {{Cl|GOTO}} resnxt
{{Cl|KILL}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
+
            {{Cl|KILL}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
{{Cl|ON ERROR}} {{Cl|GOTO}} 0
+
            {{Cl|ON ERROR}} {{Cl|GOTO}} 0
{{Cl|NAME}} filename$ {{Cl|AS}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
+
            {{Cl|NAME}} filename$ {{Cl|AS}} {{Cl|MID$}}(filename$, 1, {{Cl|INSTR}}(filename$, ".") - 1) + ".org"
{{Cl|NAME}} "filename.tmp" {{Cl|AS}} filename$
+
            {{Cl|NAME}} "filename.tmp" {{Cl|AS}} filename$
{{Cl|PRINT}} : {{Cl|PRINT}} "File conversion completed. Press any key to end."
+
            {{Cl|PRINT}} : {{Cl|PRINT}} "File conversion completed. Press any key to end."
{{Cl|EXIT DO}}
+
            {{Cl|EXIT DO}}
{{Cl|END SELECT}}
+
    {{Cl|END SELECT}}
 
{{Cl|LOOP}}
 
{{Cl|LOOP}}
 
{{Cl|SLEEP}}
 
{{Cl|SLEEP}}
Line 89: Line 110:
  
 
tracknumber:
 
tracknumber:
DO
+
{{Cl|DO}}
count = count + 1
+
    count = count + 1
linenumber(count) = {{Cl|VAL}}({{Cl|MID$}}(a$, n))
+
    linenumber(count) = {{Cl|VAL}}({{Cl|MID$}}(a$, n))
{{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}}") <> 0 {{Cl|OR (boolean)|OR}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}}") <> 0 {{Cl|THEN}}
+
    {{Cl|IF...THEN|IF}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOSUB}}") <> 0 {{Cl|OR (boolean)|OR}} {{Cl|INSTR}}(a$, "ON") <> 0 {{Cl|AND (boolean)|AND}} {{Cl|INSTR}}(a$, "{{Cl|GOTO}}") <> 0 {{Cl|THEN}}
{{Cl|FOR...NEXT|FOR}} i = n {{Cl|TO}} {{Cl|LEN}}(a$)
+
        {{Cl|FOR...NEXT|FOR}} i = n {{Cl|TO}} {{Cl|LEN}}(a$)
{{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, i, 1)) = 0 {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "0" {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "," {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> " " {{Cl|THEN}} {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
+
            {{Cl|IF...THEN|IF}} {{Cl|VAL}}({{Cl|MID$}}(a$, i, 1)) = 0 {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "0" {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> "," {{Cl|AND (boolean)|AND}} {{Cl|MID$}}(a$, i, 1) <> " " {{Cl|THEN}} {{Cl|EXIT FOR}}
{{Cl|IF...THEN|IF}} {{Cl|MID$}}(a$, i, 2) = ", " {{Cl|THEN}} n = i + 1: {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}}
+
            {{Cl|IF...THEN|IF}} {{Cl|MID$}}(a$, i, 2) = ", " {{Cl|THEN}} n = i + 1: {{Cl|EXIT FOR}}
{{Cl|NEXT}}
+
        {{Cl|NEXT}}
{{Cl|IF...THEN|IF}} n <> i + 1 {{Cl|THEN}} {{Cl|EXIT DO}}
+
        {{Cl|IF...THEN|IF}} n <> i + 1 {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|ELSE}}
+
    {{Cl|ELSE}}
{{Cl|EXIT DO}}
+
        {{Cl|EXIT DO}}
{{Cl|END IF}}
+
    {{Cl|END IF}}
 
{{Cl|LOOP}}
 
{{Cl|LOOP}}
 
{{Cl|RETURN}}
 
{{Cl|RETURN}}
Line 110: Line 131:
  
  
''See also:''
+
{{PageSeeAlso}}
 
* [[GOTO]] , [[RUN]]
 
* [[GOTO]] , [[RUN]]
 
* [[GOSUB]] , [[RETURN]]
 
* [[GOSUB]] , [[RETURN]]

Latest revision as of 10:07, 21 February 2021

Line identifiers are used to identify a specific line location in a program. There are two types of identifiers, line numbers and alphanumeric line labels.

Line identifiers act as targets for statements that alter the normal flow of control of a program such as GOTO and GOSUB.


Syntax

lineNumber [statement]
lineLabel: [statement]


Description

  • Line numbers are any combination of digits.
  • Line labels are any combination of letters and digits, starting with a letter, and followed by a colon. The colon is required to differentiate them from keywords and variable names. Case is not significant for line labels.
  • Identifiers may start in any column but may only be proceeded by whitespace characters.
  • Line numbers and labels may be freely mixed in a given program.
  • The order of line numbers is not significant and there is no correspondence between line numbers and the actual line number, within the source code file, at which they appear. Line numbers are simply numeric line labels.
  • Statements may immediately follow line identifiers. No intervening whitespace is required.
  • Line numbers are not required for every program line as they were in earlier dialects of the BASIC language.


Examples

Example 1:

100 PRINT "This is a line number." here: PRINT "This is a line label."

Example 2: Microsoft's REMLINE.BAS program removes unnecessary line numbers where they are not required by IF...THEN, GOTO, GOSUB, ERL, RUN or RESUME.

CLS REDIM linenumber(100000) 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$, "RUN ") <> 0 THEN flag = 1: n = INSTR(a$, "RUN ") + 4: GOSUB tracknumber IF INSTR(a$, "RESUME ") <> 0 THEN flag = 1: n = INSTR(a$, "RESUME ") + 7: GOSUB tracknumber IF INSTR(a$, "RETURN ") <> 0 THEN flag = 1: n = INSTR(a$, "RETURN ") + 7: 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 _DONTWAIT "notepad filename.tmp" PRINT PRINT "Back up original file as .org and give edited file original file name? [Y/N]" DO b$ = UCASE$(INKEY$) SELECT CASE b$ CASE CHR$(27): SYSTEM CASE "N" ON ERROR GOTO resnxt KILL "filename.tmp" ON ERROR GOTO 0 PRINT PRINT "Temporary "+ CHR$(34) + "filename.tmp" + CHR$(34) + " was removed and no changes were made to original file." PRINT PRINT "Press any key to end." EXIT DO CASE "Y" 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. Press any key to end." EXIT DO END SELECT LOOP SLEEP 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 Network 54 QBasic Forum


See also



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