Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my Loop end after 11 Loops?
#21
I'm certain the "recur" at the end of that code is where the LOOP goes.  Dimster didn't post BASIC; just pseudocode for his process -- or else it'd error out on the Open the file segment.  Wink
Reply
#22
It's was just pseudocode. The actual working Recursion with Do Loop does have "Loop" before the recursion call. And thanks Steve for the SEEK stuff on the other thread, definitely right up my alley. I'm still not sure why a bare recursion with a reset of the pointer using Seek only came up with 11 loops, whereas a Recursion of a Do Loop with that same Seek did all the looping required. So, I'm assuming Steve's comment about Seek working with Input may also need the qualifier Seek/Do/Input work together. A Seek/Recursion may have issues but that brings to mind the old adage of the blind man feeling the leg of an elephant and deducing it's a tree, so right can I really be that Seek/Recursion are not compatible?
Reply
#23
(02-07-2023, 08:14 PM)SMcNeill Wrote: I'm certain the "recur" at the end of that code is where the LOOP goes.  Dimster didn't post BASIC; just pseudocode for his process -- or else it'd error out on the Open the file segment.  Wink

Here's my check:
   

I haven't a clue where Dimster can put a loop without messing up recursion  Huh
b = b + ...
Reply
#24
Here ye go!
Code: (Select All)
Open "The file" For Input As #1
Recur
Sub Recur
    '  Seek #1, 1  <<< this is going to mess you up with infinite loop
    If Not EOF(1) Then
        For i = 1 To 7
            'Input the 7 data items
        Next
        DataCount = DataCount + 1
        'Call to the Subroutine to deal with the 7 data items
        do7
        ' If DataCount = 4000 Then Exit Sub ' don't need this restriction either! allow any amount data
        Recur
    Else
        Close #1
        'and report to the principle
    End If
End Sub

Sub do7
End Sub
b = b + ...
Reply
#25
Hi guys and gals
...
the thread is interesting but also just a little unclear...

Dimster posted his ideas as pseudocode....

the main part of answers posted here talks in BASIC code...

Dimster question was about "why turning  my loop into a recursive code I don't get the same result?"

A SUB callilng itsself until a flag doesn't get a specific condition is quiet different from looping with DO/LOOP.

A  SUB is just a closed block of code... so you must assure that variables or values needed for ending the job pass from the SUB calling and the SUB called.

looking closest to this pseudocode
Code: (Select All)
Open File
Recur

Sub Recur
LoopRecur = LoopRecur + 1
Seek #1, 1
for i = 1 to 7:Input DataItem(i):Next
DataCount = DataCount + 7
Call Subroutine to work on these 7 data items
if DataCount < 4000 then Recur
End Sub

1. the SUB Recur has no parameters
2. the variables  used as flags are NOT global --> at first run Recur has LoopRecur = 0 and then it becomes 1, and so the same at second run because LoopRecur is local and not GLOBAL (shared) and not passed as parameter
3. I don't understand the Seek #1,1  (coming back at the first byte of the file) but maybe it needs to do the job
4. I imagine that in pseudocode the FOR NEXT loop uses a INPUT #1, DataItem(i), in other words it reads from the file
5. (Mutatis mutandis)  at first run of the sub DataCount is 0 and it becomes 1, but so at second run and so on because it is local and not global
6. the IF statement would be infinite because DataCount will not become 4000 ....
7. in the pseudocode with DO/LOOP it uses EOF in reading the file, in that pseudocode with Recur sub this control disappears.

If I have some time I'll write the code to emulate the pseudocode of Dimster without gaining the thousands of lines!
Reply
#26
(02-07-2023, 08:49 PM)bplus Wrote:
(02-07-2023, 08:14 PM)SMcNeill Wrote: I'm certain the "recur" at the end of that code is where the LOOP goes.  Dimster didn't post BASIC; just pseudocode for his process -- or else it'd error out on the Open the file segment.  Wink

Here's my check:


I haven't a clue where Dimster can put a loop without messing up recursion  Huh

Like this:
Code: (Select All)
OPEN "data.txt" FOR INPUT AS #1
DIM SHARED DataCount AS LONG
Recur
CLOSE

SUB Recur
    SEEK #1, 1
    DO WHILE NOT EOF(1)
        FOR i = 1 TO 7
            INPUT #1, item(i)
            DataCount = DataCoount + 1
        NEXT
        DO7
        IF DataCount = 4000 THEN EXIT SUB
    LOOP
END SUB

SUB DO7
END SUB

There's no recursion needed here; just a SEEK at the start to make certain that he starts reading from the beginning of the file.

To do it with recursion, the process would be like I posted back on the first page:
Code: (Select All)
DIM SHARED AS LONG DataCount, DataItem(7)
OPEN "someFile.txt" FOR INPUT AS #1
DataCount = 1
Recur

SUB Recur
    IF DataCount = 1 THEN SEEK #1, 1 'move to the start of the file 'may need to change to 0 for 0 index counting
    FOR i = 1 TO 7
        INPUT DataItem(i)
    NEXT
    DataCount = DataCount + 7
    CALL WorkSub
    IF DataCount < 4000 THEN Recur
END SUB

SUB WorkSub
    'work on these specific data items
END SUB
Reply
#27
It woiks!
save as "The file"
Code: (Select All)
Now
is
the
time
for
all
good
men
to
use
recursion
to
fill
their
days
with
joy
or
woe
is
me.

Code: (Select All)
Open "The file" For Input As #1
Recur
Sub Recur
    'Seek #1, 1
    If Not EOF(1) Then
        b$ = ""
        For i = 1 To 7
            Input #1, fline$
            b$ = b$ + " " + fline$
        Next
        DataCount = DataCount + 1
        'Call to the Subroutine to deal with the 7 data items
        do7 b$
        'If DataCount = 4000 Then Exit Sub
        Recur
    Else
        Close #1
        'and report to the principle
        Print "The file is now closed!"
    End If
End Sub

Sub do7 (x$)
    Print x$
End Sub

Interesting note: No Static nor Shared needed! And no dang Do... Loop
b = b + ...
Reply
#28
@bplus I think you're missing the whole point here -- the objective is to read the file *without* having to OPEN it at the start, or CLOSE it at the end.  That's why there's the SEEK in there at the top, to move to INPUT pointer manually to the beginning of the file.  It's probably opened once at the start of the program, but read from in various different subroutines and functions. 

Try my simple post before yours, with your data file.  Wink
Reply
#29
The point in my eye is to use recursion to go through and process the file without a main loop.

I am sure "The file" would work in your simplest demo that does not use recursion.

Seek in my opinion is big fat Red Herring, distraction.
b = b + ...
Reply
#30
(02-07-2023, 09:36 PM)bplus Wrote: The point in my eye is to use recursion to go through and process the file without a main loop.

I am sure "The file" would work in your simplest demo that does not use recursion.

Seek in my opinion is big fat Red Herring, distraction.

Code: (Select All)
DIM SHARED AS LONG DataCount, DataItem(7)
OPEN "someFile.txt" FOR INPUT AS #1
DataCount = 1
Recur

SUB Recur
    IF DataCount = 1 THEN SEEK #1, 1 'move to the start of the file 'may need to change to 0 for 0 index counting
    FOR i = 1 TO 7
        INPUT DataItem(i)
    NEXT
    DataCount = DataCount + 7
    CALL WorkSub
    IF DataCount < 4000 THEN Recur
END SUB

SUB WorkSub
    'work on these specific data items
END SUB

No main loop? Check.
Recursion? Check.
SEEK? Check.

What's missing here??
Reply




Users browsing this thread: 2 Guest(s)