comparing objects in a collection

Torge at gmx.de Torge at gmx.de
Mon Feb 3 10:37:53 UTC 2003


Hi Janet,

I think your mistake lies in what you expect to happen.
The comparison wil be evaluated for every element in the collection
and thus the transcipt will tell you (n-1) times that the element is
not in the collection (where n is the number of elements in the collection)
and only once will show the element. 

To make it clearer i'd change the code to:
(assuming you do this in an inspector where self is bound to the
collection)
self do:[:element | element number == accountNumber
	ifTrue:([Transcript show: element;cr])
	ifFalse:([Transcript show: element, ' is not the desired element';cr])].

Of course you could also write:
Transcript show:
	(self
		detect:[:element | 
			element number == accountNumber]
		ifNone:['no such element']).

HTH,
Torge


03.02.2003 05:44:13, Janet Abdul-Karim <jn_karim at yahoo.com> wrote:
>  > findAccount: accountNumber
>  > "Find the account with the argument number"
>  >
>  > self do:[:element | element number == accountNumber
>  > ifTrue:([Transcript show: element;cr])
>  > ifFalse:([Transcript show: 'No such account exists in this
>  portfolio';cr])].
>
>  My guess is that you want to use #= (equality) not #== (identity
>  equality). Numbers above a certain size are represented in such a way
>  that they are not the same object even when they are the same number, and
>  so they are #= but not #==. For example:
>
>  12 = 12 "true"
>  12 == 12 "true"
>  (12 raisedTo: 10) = (12 raisedTo: 10) "true"
>  (12 raisedTo: 10) == (12 raisedTo: 10) "false"
>
>  Avi
>
>
>
>
>
>
>  Do you Yahoo!?
>  Yahoo! Mail Plus - Powerful. Affordable. Sign up now





More information about the Squeak-dev mailing list