|
Post by jarychk on Jul 17, 2024 4:02:17 GMT
I cannot find a way to make WHILE loop reliably put string expressions into an array. I thought I had something but nothing works right.
Take like I have L$ for putting in the information from the DATA line, and to stop if the last item is "end". and then put all the items into array.
READ L$ and have DIM thelist() array
Someplace in the program need to have
DATA "firstitem","seconditem","thirditem","fourthitem","end"
The item called "end" must not be put into the array and must not be read.
I could do with a FOR loop, but that's not what I want to do.
|
|
|
Post by Rod on Jul 17, 2024 6:57:30 GMT
You need to read the end but not write it to the file. So the trick is to read the data, find end then stop. On a while wend that means you write to file conditionally ie it’s not end. If you only while,write,read,wend that works to but you need to pre read the very first item before you enter the while wend loop.
|
|
|
Post by jarychk on Jul 17, 2024 7:32:17 GMT
You need to read the end but not write it to the file. So the trick is to read the data, find end then stop. On a while wend that means you write to file conditionally ie it’s not end. If you only while,write,read,wend that works to but you need to pre read the very first item before you enter the while wend loop. I found something like that, but not exactly. I show this which apparently works the way I hoped: Member or other visitor should be aware, this small example creates a sequential text file, so user can and should check in that file to see if the code worked. dim bsample$(100) B$=""
'try a simple decision structure inside WHILE loop i=0 while B$<>"end" read B$ if B$<>"end" then i=i+1 'yes, HERE! bsample$(i)=B$ 'i=i+1 not here! end if 'NOW you have the count of the first default list of bsample$() wend
data "itemone","itemtwo","itemthree","itemfour","end"
'put all those into file bladesamples.txt c0=0 open "bsamples.txt" for output as #1 for c0=1 to i print #1,bsample$(c0) next c0 close #1
END
The Tutorial helpfile included with Just BASIC v2.0 did help.
|
|
|
Post by tsh73 on Jul 17, 2024 8:14:06 GMT
You can use EXIT WHILE to exit on a condition
i=0 while 1 'forever loop read B$ if B$="end" then exit while 'exit WHILE if condition is met 'normal processing i=i+1 bsample$(i)=B$ wend
Or you could prime the loop (read first data item before the loop) and have only condition in WHILE
i=0 read B$ 'read first one
while B$<>"end" i=i+1 bsample$(i)=B$ read B$ 'read next one wend
|
|
|
Post by plus on Jul 17, 2024 23:50:39 GMT
Another way to load theList$()
dim theList$(4) theList$(1) = "first item" theList$(2) = "second item" theList$(3) = "third item" theList$(4) = "fourth item"
b = b + ...
|
|
|
Post by jarychk on Jul 18, 2024 1:14:06 GMT
Another way to load theList$() dim theList$(4) theList$(1) = "first item" theList$(2) = "second item" theList$(3) = "third item" theList$(4) = "fourth item" b = b + ... Excellent and so simple. I really did want and finally used a WHILE loop and READ-DATA. It needed some logical care. I will review offline what I did to see if I can find the simple example. Your way what you showed is much easier. edit: YES I posted the successful code a day ago. WHILE Loop with a decision structure in the loop. Main reason for that code is maybe if method is applied in an actual application, I might want to use READ & DATA for several more than just four items.
|
|
|
Post by plus on Jul 18, 2024 14:37:11 GMT
Yes the direct assignments methods is only practical for maybe under a dozen items. The While method is excellent for loading data from a file into your program array.
b = b + ...
|
|