Page 3 of 3

Re: A Walrus :=

Posted: Wed Sep 27, 2023 8:43 am
by Little John
the.weavster wrote: Wed Sep 27, 2023 7:45 am The more versatile the better imo. Just expanding a little on the OpenFile() example:

Code: Select all

If file := OpenFile(#PB_Any,  "file.txt")
	While text.s := ReadString(file)
		; do work with text
	Wend
	CloseFile(file)
EndIf
That is a very special way of reading a text file, since the loop will stop reading at the first empty line.

The general way of reading a text file is like so: :wink:

Code: Select all

If file := OpenFile(#PB_Any,  "file.txt")
   While Not Eof(file)
      text.s = ReadString(file)
		; do work with text
	Wend
	CloseFile(file)
EndIf
Here the Walrus operator cannot be used with advantage for reading the lines in the loop.

Re: A Walrus :=

Posted: Wed Sep 27, 2023 8:59 am
by the.weavster
Little John wrote: Wed Sep 27, 2023 8:43 am That is a very special way of reading a text file, since the loop will stop reading at the first empty line.
You're right ...

Code: Select all

text.s = ""
If text
  Debug "true"
Else
  Debug "false"
EndIf

Re: A Walrus :=

Posted: Thu Sep 28, 2023 3:53 pm
by Quin
Little John wrote: Wed Sep 27, 2023 8:43 am
the.weavster wrote: Wed Sep 27, 2023 7:45 am The more versatile the better imo. Just expanding a little on the OpenFile() example:

Code: Select all

If file := OpenFile(#PB_Any,  "file.txt")
	While text.s := ReadString(file)
		; do work with text
	Wend
	CloseFile(file)
EndIf
That is a very special way of reading a text file, since the loop will stop reading at the first empty line.

The general way of reading a text file is like so: :wink:

Code: Select all

If file := OpenFile(#PB_Any,  "file.txt")
   While Not Eof(file)
      text.s = ReadString(file)
		; do work with text
	Wend
	CloseFile(file)
EndIf
Here the Walrus operator cannot be used with advantage for reading the lines in the loop.

Code: Select all

If file := OpenFile(#PB_Any,  "file.txt")
	If Text.s := ReadString(file, #PB_File_IgnoreEOL) and Text <> ""
		; do work with text
	EndIf
	CloseFile(file)
EndIf
:)

Re: A Walrus :=

Posted: Thu Sep 28, 2023 5:02 pm
by Little John
Of course, all kinds of contortions are possible ...