[Newbies] Re: last n entries of an OrderedCollection

Andreas Raab andreas.raab at gmx.de
Mon Apr 12 03:22:29 UTC 2010


On 4/11/2010 8:14 PM, sergio_101 wrote:
> wondering of there is a built in function to spit out the last n
> entires of an ordered collection..
> in a pinch, this worked:
> | ipcount newList |
> ipcount := self repository size.
> newList := OrderedCollection new.
> (ipcount - anInteger) to: ipcount do: [ :n | newList add: (self
> repository at: n) ].
>
> but i am guessing that is a total kludge...

Use #last: e.g.,

	collection := (1 to: 50) asOrderedCollection.
	collection last: 5.            "45 .. 50"

There is also #first: and the companions #allButFirst: and #allButLast:, 
e.g.,

	collection first: 5.           "1 .. 5"
	collection allButFirst: 5.     "6 .. 50"
	collection allButLast: 5.      "1 .. 45"

And of course these work with strings too, thus:

	'image.bmp' last: 4. 		".bmp"
	'image.bmp' allButLast: 4.	"image"

Cheers,
   - Andreas

Cheers,
   - Andreas


More information about the Beginners mailing list