comparing objects in a collection

Bill Spight bspight at pacbell.net
Mon Feb 3 06:15:50 UTC 2003


Dear Janet,

> 
> I am trying to find a certain value in a collection.  does something
> if true or does something if false and for some reason it always
> evaluates to false and I know its there.  Any help would be great
> 
> sample code.
> 
> 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])].
> 
> element is an object that has number as a method
> 

You say, "sample code." Is this your exact code? This code should print
out a message for each account. Is that what happens? If so, you should
have "No such account exists .... " printing out for all accounts but
one. From what you say, it sounds like it prints something out only
once.

Perhaps your code looks at only one element. If element number ==
accountNumber, it prints the element in Transcript, if not, it says
(falsely), "No such account exists in this portfolio."

You might try something like this:

findAccount: accountNumber
 "Find the account with the argument number"

 self do:[:element | element number == accountNumber 
  ifTrue:[Transcript show: element;cr. ^element]].
"If there is a match, the method returns element."
 Transcript show: 'No such account exists in this portfolio';cr.
 ^false
"If there is no match, it returns false."

Good luck!

Bill



More information about the Squeak-dev mailing list