Smalltalk write a Sorted Collection to a file

廉价感情. 提交于 2019-12-11 03:46:48

问题


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 Customers 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!