Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top