A Walrus :=

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: A Walrus :=

Post 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.
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: A Walrus :=

Post 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
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: A Walrus :=

Post 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
:)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: A Walrus :=

Post by Little John »

Of course, all kinds of contortions are possible ...
Post Reply