问题
let's say i have a SortedCollection called whitepages
, each record containing a Customer
, with values name
and number
.
I want to create a function which would write them to a file in such way
name1
number1
name2
number2
I have to admit Im completely stuck here. Could anyone help?
回答1:
Ok, so I hope you know how to write to file.
And as you noticed that you have sorted collection I suppose that Customer
s are ordered in the way you want.
Then what you can do is:
(whitepages collect: [ :customer |
customer name,
Character cr asString,
customer number ]) joinUsing: Character cr
This way you'll get a string that you just need to write to the file. Also note that if name
or number
are not strings you can use asString
on them.
The canonical way is to do something like:
whitepages do: [ :customer |
stream
nextPutAll: customer name;
cr;
nextPutAll: customer number;
cr ]
Where stream
is a write stream to a file.
来源:https://stackoverflow.com/questions/20228668/smalltalk-write-a-sorted-collection-to-a-file