comparing objects in a collection
Roel Wuyts
roel.wuyts at iam.unibe.ch
Mon Feb 3 09:34:34 UTC 2003
Have you tried stepping through this with the debugger? Below some
ideas to help you solving problems like this the next time (how to set
breakpoints, have breakpoints with comment and do conditional
breakpoints). If you already know this, then it always might help
someone else :-)
For example, add a breakpoint by sending a 'halt' message to any kind
of object. Then you can step and send through this piece of code to see
what exactly goes on:
self do: [:element | self halt. element number == accountNumber
ifTrue:([Transcript show: element;cr])
ifFalse:([Transcript show: 'No such account exists in this
portfolio';cr])].
You will see that the message == gets send and what it does. Note that
you can accept new code while you go along, and it will immediately
take tis into account. Try it to believe it :-)
Note also that you can use 'halt:' and give a string argument that will
be shown when the breakpoints pops up. For example the following will
pop up the current number it needs to compare. That way you can decide
whether to do 'proceed' or 'debug' more easily:
self do: [:element | self halt: element printString. element number ==
accountNumber
ifTrue:([Transcript show: element;cr])
ifFalse:([Transcript show: 'No such account exists in this
portfolio';cr])].
You can easily set conditional breakpoints, since it is just plain
Smalltalk code. For example, you might only want to see the cases when
numbers are negative like this:
self do: [:element | (element < 0) ifTrue: [self halt: element
printString]. element number == accountNumber
ifTrue:([Transcript show: element;cr])
ifFalse:([Transcript show: 'No such account exists in this
portfolio';cr])].
With the same mechanism you can pop up the debugger when a certain key
is pressed (so that you can let it crunch for a while before going in
to inspect the code. I do not know the exact expression in Squeak, but
somebody else will be able to help you there. In VisualWorks (another
Smalltalk environment) you would say:
InputState default shiftDown ifTrue: [self halt].
Hope this helps a bit.
But I am pretty sure (like Avi was) that you want to compare using #=
in this case. Almost all of the time you want to do that, actually,
unless you are really looking for a particular object, and only want
that object.
On Monday, February 3, 2003, at 05:44 AM, Janet Abdul-Karim wrote:
> I am understanding what you said about the difference between #= and
> #==, but I still get the same result. I am wondering is my logic
> somehow work or could I possibly be missing some type of punctuation
> because I know the object is there but it seems to always evaluate the
> false part and then prints out the type of the object that i am
> looking for.
>
> Avi Bryant <avi at beta4.com> wrote:
>
>
> On Sun, 2 Feb 2003, Janet Abdul-Karim wrote:
>
> >
> > 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])].
>
> 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: Yahoo! Mail Plus - Powerful.
> Affordable. Sign up now
>
>
>
Roel Wuyts Software
Composition Group
roel.wuyts at iam.unibe.ch University of Bern,
Switzerland
http://www.iam.unibe.ch/~wuyts/
Board Member of the European Smalltalk User Group: www.esug.org
More information about the Squeak-dev
mailing list
|