help reading a string/int pair

This forum is dedicated to those who want support programming in ugBASIC.
Questo forum è dedicato a chi desidera supporto per programmare in ugBASIC.
Ce forum est dédié à ceux qui souhaitent prendre en charge la programmation en ugBASIC.
Post Reply
jjarrell
Posts: 36
Joined: Tue Jan 28, 2025 5:35 pm

help reading a string/int pair

Post by jjarrell »

I get an error when compiling this because of the string variable in the READ statement and I assume it has to defining the DATA as a type.
How should I correct this to read in the data string/integer pair?

Code: Select all

READ item$, qty
PRINT item$,qty
DATA "BLUE WIDGET",10
ervin
Posts: 10
Joined: Wed Jul 03, 2024 12:41 pm

Re: help reading a string/int pair

Post by ervin »

Just wondering...
Would something like this help?
(I'm at work so I can't check if it runs)

Code: Select all

READ item$, qty
PRINT item$,qty
DATA AS STRING "BLUE WIDGET"
DATA AS BYTE 10
spotlessmind1975
Site Admin
Posts: 200
Joined: Fri Oct 06, 2023 8:25 pm

Re: help reading a string/int pair

Post by spotlessmind1975 »

Hi jjarrell and ervin!
jjarrell wrote: Thu Feb 06, 2025 10:42 pm I get an error when compiling this because of the string variable in the READ statement and I assume it has to defining the DATA as a type.
How should I correct this to read in the data string/integer pair?
First of all, thank you for the code fragment. Actually the compilation error was a real compiler error that I fixed in this evening's COLDFIX (COLDFIX v1.17 [rev. 20250207]), and I'll try to explain the rationale.

First of all, let's start from the assumption that BASIC (and therefore ugBASIC) do not need to define variables before being used. This is due to the so-called "implicit definition". However, the implicit definition does not carry the type with it. To overcome this limitation, you can use type suffixes, as you (rightly) wrote. And here comes the error: the syntax did not provide for the use (even optional) of the type when indicating the identifier for the READ command. So your example was correct.
ervin wrote: Thu Feb 06, 2025 10:51 pm Would something like this help?
Almost. In fact, this syntax can be useful for optimizing the code produced (as long as one uses the DEFINE READ FAST directive). However, in this case one must be sure of the type one is reading, otherwise the program may break.

Thank you again!
jjarrell
Posts: 36
Joined: Tue Jan 28, 2025 5:35 pm

Re: help reading a string/int pair

Post by jjarrell »

Good news. It compiles with no errors and prints the QTY value.

However, there seems to be some odd behavior in the way it treats the QTY variable.
With the example below if you use the freshly read qty in a for/next, QTY appears to be treated as a 0.
When it is part of a calculation, total=total+qty, it adds it correctly.
If you give QTY a value, qty=10, it will work in the for/next loop.

What do I need to do to get the READ QTY to work in the for/next loop?

Code: Select all

CLS
PRINT "INVENTORY"
PRINT "---------"
WHILE NOT READ END
	READ item$,qty
	PRINT item$,qty;
	REM qty GETS TREATED AS 0
	FOR i=1 TO qty
		PRINT "O";
	NEXT
	PRINT
	REM ADDS TO TOTAL JUST FINE
	total=total+qty
WEND
PRINT
PRINT "TOTAL: ";total
PRINT
PRINT "COUNT OF LAST ITEM: ";qty
REM JUST ANOTHER TEST AND qty IS STILL TREATED AS 0
FOR i=1 TO qty
		PRINT "O";
NEXT
PRINT
PRINT
REM WORKS PERFECTLY IF YOU MANUALLY ASSIGN A NUMBER
qty=10
FOR i=1 TO qty
		PRINT "X";
NEXT
PRINT
WAIT KEY RELEASE
END
DATA "SWORDS",10
DATA "BOWS",5
DATA "ARROWS",15
DATA "DAGGERS",8
DATA "SPEARS",6
spotlessmind1975
Site Admin
Posts: 200
Joined: Fri Oct 06, 2023 8:25 pm

Re: help reading a string/int pair

Post by spotlessmind1975 »

Hi jjarrell, thank you for the feedback and the example!
jjarrell wrote: Mon Feb 10, 2025 4:39 pm What do I need to do to get the READ QTY to work in the for/next loop?
Sorry for the problem. It was a fault of mine. I introduced a regression, in order to save space on code generation for FOR...NEXT. You need to update your compiler, since an HOTFIX has been published today for cpc target (to fix bug#1195), and I just built the one for coco3. Sandbox has been fixed, too, so you could try also on that.

Thank you again!
jjarrell
Posts: 36
Joined: Tue Jan 28, 2025 5:35 pm

Re: help reading a string/int pair

Post by jjarrell »

Thanks for your help. It works as intended.
Also, thanks Ervin your suggestions.

I've cleaned up the example and posting it in the snippet area.
Post Reply