Author Topic: How to Avoid Conditional DATA line READ Errors?  (Read 3021 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
How to Avoid Conditional DATA line READ Errors?
« on: June 19, 2019, 01:28:06 pm »
In the code given here, the first line containing a DATA statement is inside a condition which only happens the first time the code is run.  At any subsequent program run, it is required to ignore that DATA line.

However, when READing for the Parameters!() array input (the subsequent DATA lines), the first elements of Parameters!() are taken from that first DATA line, even though that is not executed.  Is there a way to skillfully avoid that?  In my case, it was easy enough to move the DATA lines for Parameters!() before the original DATA line.

Code: QB64: [Select]
  1. IF _FILEEXISTS("ducks.cfg") THEN
  2.     OPEN "ducks.cfg" FOR INPUT AS #1
  3.     FOR N%% = 0 TO 8
  4.         INPUT #1, KeyCode%(N%%)
  5.     NEXT N%%
  6.     CLOSE #1
  7.     DATA 119,122,97,100,112,46,108,39,115 ' This line is READ even when this condition is not executed
  8.     FOR N%% = 0 TO 8
  9.         READ KeyCode%(N%%)
  10.     NEXT N%%
  11.  
  12. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  13. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  14. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  15. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  16. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0.005
  17. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0.008
  18. FOR N%% = 1 TO 6
  19.     FOR M%% = 0 TO 15
  20.         READ Parameters!(N%%, M%%)
  21.     NEXT M%%
  22. NEXT N%%

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to Avoid Conditional DATA line READ Errors?
« Reply #1 on: June 19, 2019, 02:18:04 pm »
Hi. Something as this?

Code: QB64: [Select]
  1.     data_block_a$ = "": data_block_b$ = ""
  2.     IF INT(TIMER) MOD 2 = 0 THEN
  3.         RESTORE data_block_a
  4.         READ data_block_a$
  5.     ELSE
  6.         RESTORE data_block_b
  7.         READ data_block_b$
  8.     END IF
  9.     PRINT data_block_a$, data_block_b$
  10.  
  11.  
  12. data_block_a:
  13. DATA "before"
  14.  
  15. data_block_b:
  16. DATA "after"
  17.  
  18.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to Avoid Conditional DATA line READ Errors?
« Reply #2 on: June 19, 2019, 02:37:28 pm »
so directly to your code:

Code: QB64: [Select]
  1. IF _FILEEXISTS("ducks.cfg") THEN
  2.     OPEN "ducks.cfg" FOR INPUT AS #1
  3.     FOR N%% = 0 TO 8
  4.         INPUT #1, KeyCode%(N%%)
  5.     NEXT N%%
  6.     CLOSE #1
  7.     file_not_exists:
  8.     DATA 119,122,97,100,112,46,108,39,115: ' This line is READ even when this condition is not executed  ADDED COLON!
  9.     RESTORE file_not_exists
  10.     FOR N%% = 0 TO 8
  11.         READ KeyCode%(N%%)
  12.     NEXT N%%
  13.  
  14.  
  15. data_block_2:
  16. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  17. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  18. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  19. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0
  20. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0.005
  21. DATA 0.25,30,1E5,0.02,0.002,0.02,0.01,50,5,50,100,30,5,50,100,0.008
  22.  
  23. RESTORE data_block_2
  24. FOR N%% = 1 TO 6
  25.     FOR M%% = 0 TO 15
  26.         READ Parameters!(N%%, M%%)
  27.     NEXT M%%
  28. NEXT N%%
  29.  
  30.  

and one warning for you. If you use DATA, then character ' must be separated by a colon. Otherwise, READ takes it as part of a DATA block.
« Last Edit: June 19, 2019, 02:42:29 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How to Avoid Conditional DATA line READ Errors?
« Reply #3 on: June 19, 2019, 03:13:25 pm »
Data statements are best placed after main code block - from Wiki

As I remember READ looks for first DATA line regardless of where it sits in program, unless RESTORE is used to set the next DATA READ line.

You can isolate DATA statements in a SUB or FUNCTION only to be READ inside that procedure.




Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: How to Avoid Conditional DATA line READ Errors?
« Reply #4 on: June 19, 2019, 07:32:27 pm »
Yeah, there is no such thing as 'Conditional Data' as far as I'm aware. the best way to accomplish what it looks like your trying to is use a RESTORE command within the condition to read specific data area. unless its called from within a SUB or FUNCTION as BPlus has said, data is read from top down within the main code. Perhaps a better way is to preload all the data into an array and just call upon the required array area when needed.
Granted after becoming radioactive I only have a half-life!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: How to Avoid Conditional DATA line READ Errors?
« Reply #5 on: June 20, 2019, 04:00:17 am »
Thanks guys.  Look like I should have read the wiki.