Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

OO search in objects

by Hossein (Acolyte)
on Aug 14, 2013 at 11:49 UTC ( #1049421=perlquestion: print w/ replies, xml ) Need Help??
Hossein has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a set of objects in an array. @obj_fw

When I search for an object, I run through the list @obj_fw, and check if f.ex. $obj_fw[$index]->name eq search-string.

I wonder if there's a better way of doing it? Like a nice method in the package. In this case MY::FW (FW.pm)

Thank you :)

Comment on OO search in objects
Download Code
Re: OO search in objects
by Happy-the-monk (Monsignor) on Aug 14, 2013 at 12:09 UTC

    I wonder if there's a better way of doing it?

    It depends... Assuming there are no duplicate names, I would create a hash once populated with the names as keys and the object refs as values.

    my %objects_by_name; for (@obj_fw) { $objects_by_name{$_->name} = $_ }

    or even better right when the objects are being created/given names.

    From then on you get the objects by name from $objects_by_name{$name}

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: OO search in objects
by sundialsvc4 (Monsignor) on Aug 14, 2013 at 13:06 UTC

    If your program most-commonly looks for objects by “name,” then putting the objects into a hash rather than array might be most effective.   The each() iterator can be used to walk through it, or you can use keys to obtain an array of keys.   This will only (easily ...) work if the names are unique.

    You can also have it both ways.   The “array of objects” actually contains references to those objects (as does the hash above), and it’s perfectly find to have more-than-one reference to the same thing.   This is directly analogous to an SQL database with its multiple “indexes.”   It works great, with only one small catch:   you must know that this is what you’ve done.   If you want to dispose of an object, you do so by eliminating all references to it ... from the array, and also from the hash.

Re: OO search in objects
by NetWallah (Abbot) on Aug 14, 2013 at 14:17 UTC
    What I frequently do is to add a "my @collection" at the top of the class (package) declaration.

    I know this would upset a few OO-purists, but it works.

    In the "new" method, before "return $self", do "push @collection, $self".

    Now, you can create an object method that searches @collection.

    In your case, you would probably be better off with %collection.

    The alternative is to create a separate "collection" package that holds all the individuals.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

      this would upset a few OO-purists
      Well, not only purists. How does it work when you subclass the class?
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Subclassing is something programming books teach as immensely important, but is actually pretty damn rare in the real world...

        Forget subclassing ... how does it work if you want to have two or more collections?

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

        There are several, admittedly hack-y options for subclassing:

        1. Call the parent-class "new" method, and let it collect
        (Assumes you dont need subclass collections)
        2. Do a similar @collection in the subclass
        3. Write the parent class such that you have a reference inside the object : something like:
        $self{COLLECTION} = \@collection;
        then manipulate that property in the subclass.

        Disclaimer: I have never had the need to subclass these, so this is untested.

                     My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049421]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-08-29 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls