Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: OO Identity Crisis

by herveus (Prior)
on Jun 20, 2005 at 15:34 UTC ( [id://468389]=note: print w/replies, xml ) Need Help??


in reply to OO Identity Crisis

Howdy!

Another way to look at the problem is "how do I find a given node in the collection?"

If the items in the collection are unblessed scalars, you can compare them directly. The more "interesting" case is where the items are blessed scalars (or "objects"). The default comparison is "==". If they come up with the same address, they are the same thing. If ref(x) ne ref(y), the items are probably not equal, but your situation may permit objects of different classes to still come up "equal". Any deeper comparison *must* (as you note) be a method on the object.

It's already been noted that Java provides an "equals" method in the uber-superclass "Object", so you can always invoke .equals on an object. Something along those lines for your node class(es) would be indicated. Pick an appropriate name for the method and go forth.

is_duplicate could be a wrapper around a find() method that returns the given object if it is in the collection, or some other useful value. find() could be responsible for iterating over the collection in an appropriate manner, applying the comparison function.

yours,
Michael

Replies are listed 'Best First'.
Re^2: OO Identity Crisis
by snoopy (Curate) on Jun 21, 2005 at 07:00 UTC
    >> If the items in the collection are unblessed scalars, you can compare them
    >> directly. The more "interesting" case is where the items are blessed scalars
    >> (or "objects"). The default comparison is "==".

    Class::DBI lets you do a simple "eq" test for sameness of database objects.

    It uses overload to return an encoded key value in a string context, so that $a eq $b works anywhere that primary keys have been declared and both are from the same table.

    Update: This overloading leads to a trivial Schwartzian Transform: @t = map { [$_, "$_"] } @u, potentially making any sorting or duplicate checking lightening fast!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://468389]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-24 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found