First time here? Check out the FAQ!
x

smalltalk - readStream previous character?

0 votes
299 views
I am very much enjoying Jon Bellona's work on csv files, and the "Make a StepSequencer from a Text file" script, which I'm copying below. In the while loop, I have a question about readStream, and the line "nbr := f next asInteger". I'd like to remember the previous character in the file, and add the current character to it as a string. Looking around a bit, I thought that something like this might work - "foo := f skip: -1." But it crashes Kyma. Is there a way to read the previous character(s) from the readStream command?

Thanks!

----------------------

| fileName file f  nbr pitches durs |

"Type your text file name here inside the single quotes, and set MM to the beats per minute."
fileName := 'test.txt'.

file :=
    Preferences
        locateFileNamed: fileName
        ofType: HostDriverInterface textFileType
        locateMessage: ' locate the text file named '.
file == nil ifTrue: [^ self abortForKyma].

f :=  file asFilename readStream.

Transcript cr; show: f size printString.
pitches := OrderedCollection new.
durs := OrderedCollection new.

[f atEnd] whileFalse: [
    nbr := f next asInteger.
    (nbr < 97) ifTrue: [durs add: 1] ifFalse: [durs add: 0.25].
    pitches add: (nbr max: 20)].
seq start: 0 s length: f size pitches: pitches durations: durs.

f close.
asked Jun 24, 2016 in Capytalk & Smalltalk by stephen-taylor (Adept) (1,620 points)

1 Answer

+2 votes

Stephen, "next asInteger" transforms the character into its ASCII value (e.g. space = 32, A=65, etc.)

To remember the previous, you could add a separate variable (e.g. 'prevnbr') and store 'nbr' into this var at the end of the loop (e.g. prevnbr := nbr.), so you would have both current and previous values available to you as you set up the arrays.

As to combining previous with current characters into a String, I do not have a direct answer.  Do you want these as integers instead of characters?  If you want as ints, you can just add 'prevnbr' and 'nbr' together. I've attached a quick example using the code above that just displays the current and previous values and adds them together. these display in a debug window so you can see.

http://kyma.symbolicsound.com/qa/?qa=blob&qa_blobid=327205634452730059

answered Jun 24, 2016 by jonbellona (Adept) (1,300 points)
Thanks Jon - I can't believe I didn't think of storing the current character! Grrr. I am a strictly amateur programmer. I might have more dumb questions but I'll try to ask smarter ones.
...