Thank you for your time and answers. much appreciated.
cordially,
---- On Thu, 31 Aug 2023 05:00:00 -0400 christoph.thiede@student.hpi.uni-potsdam.de wrote ---
Hi Timothy,
if you want to use Streams, you could also send #nextFloat:
s := '2.14 -15' readStream. s nextFloat. "2.14" s nextFloat. "-15.0 "
(Shameless self-promotion: The SimulationMethodFinder [1] can help you to find such selectors - this is how I discovered this message just now.)
Other comments on your code: You only need to convert each value once into an integer if you store the result in a and b. Actually, you could even forego any conversion if you do not need validation since String>>#+ automatically handles that for you. You are using a lot of redundant brackets, only two pairs of them in your code are required. :-) Instead of string concatenation, you could also use String>>#format: again:
outstring := '{1} {2} {3}' format: {a. b. a + b}.
Happy Squeaking! Christoph
[1] https://github.com/LinqLover/SimulationStudio#simulationstudio-tools
--- Sent from Squeak Inbox Talk
On 2023-08-31T09:27:42+02:00, mailto:marcel.taeumel@hpi.de wrote:
Hi Timothy --
Try #upToAnyOf: and CharacterSet such as "Character separators":
'Hello Squeak' readStream upToAnyOf: Character separators.
Best, Marcel Am 31.08.2023 09:23:32 schrieb gettimothy <gettimothy(a)zoho.com>: Hi Folks.
Is there a standard Stream idiom to get all the data up to a space (or other separator?)
Here is my motive....
For this simple RosettaCode task https://rosettacode.org/wiki/A%2BB#Smalltalk [https://rosettacode.org/wiki/A%2BB#Smalltalk]
I need to put two numbers on a stream and display their sum on stout
The below works fine for single digit integers.
|task input |
task := [:inStream :outStream |
|processLine|
processLine :=
[
|a b space outstring|
a := (inStream next asString).
space := inStream next.
b := (inStream next asString).
"is validation part of the task?"
self assert:( a asInteger between:-1000 and: 1000).
self assert:( b asInteger between:-1000 and: 1000).
outstring := a , ' ' , b , ' ' , ((a asInteger) + (b asInteger)).
outStream store: outstring ; ensureCr; flush.
].
[ inStream atEnd ] whileFalse:processLine.
].
input := Project uiManager request: 'Enter Two Numbers Separated By a Space'.
input isEmptyOrNil ifTrue: [^ self].
task value: input readStream value: (FileStream stdout).
However, I would like to generalize this to handle multi-digit Integers and Real Numbers (i.e. decimal places, not fractions, not e, not i, not square roots, not pi, ...just 123.456 sort of stuff)
I will be adding any answers/solutions to Doc-SqueakHOWTOHelp so I have ready access to this answer when I need it again.
Thanks in advance.
["MethodFinder.png"]_______________________________________________ Beginners mailing list -- mailto:beginners@lists.squeakfoundation.org To unsubscribe send an email to mailto:beginners-leave@lists.squeakfoundation.org