Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: inside-out objects using arrays?

by borisz (Canon)
on Sep 17, 2005 at 23:13 UTC ( #492934=note: print w/replies, xml ) Need Help??


in reply to inside-out objects using arrays?

Better use a hash, since you may delete objects. Take a look at Class::Std for some hints, if you really want these objects.
Boris

Replies are listed 'Best First'.
Re^2: inside-out objects using arrays?
by rvosa (Curate) on Sep 17, 2005 at 23:34 UTC
    I was thinking the following, which would lead to undef array elements once $obj->DESTROY gets called. Still, I like it ;-)
    package Outside::In; use strict; use warnings; { my $obj_counter = 0; my @name; sub new { my $class = shift; my $instance_index = $obj_counter; my $self = sub { return $instance_index }; bless $self, $class; $obj_counter++; return $self; } sub name { my $self = shift; $name[$self->()] = shift if @_; return $name[$self->()]; } sub DESTROY { my $self = shift; delete $name[$self->()]; } } package main; my $obj1 = Outside::In->new; my $obj2 = Outside::In->new; $obj1->name('Bob'); $obj2->name('Fred'); print $obj1->name, "\n", $obj2->name, "\n"; print $obj1->();
      I was thinking the following, which would lead to undef array elements once $obj->DESTROY gets called. Still, I like it ;-)

      The point borisz was making is that with a solution like this  @name always increases in length as objects are created and deleted, which will lead to you running out of memory in the long term.

        You could avoid that problem by assigning the index to the first undef slot, rather than straight incrementing:

        my $instance_index = List::Util::first { !defined $_ } @name;

        You end up, though, with an O(n) constructor. But perhaps the savings in access make up for this.

        I know, I was sort of making that point also. Then I started thinking about how to regain the empty slots, but as fishbot_v2 mentions, that's O(N). Conceivably, you might want to take that performance hit of you subsequently do lots (and lots and lots and lots) of lookups...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://492934]
help
Chatterbox?
[james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....
[james28909]: some kind of logic behind it
[james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
[holli]: here's something for you to watch, James. I think you will like it
[erix]: for the record: I have not downvoted anyone on that subthread that was my fault
[james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
[james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
[erix]: teleology -- I've never understood why that was thunk up
[erix]: ( and when teleology was brought up, during my biology-study., I couldn't get an answer either )
[1nickt]: FTR I agree with you about "design." Just not sure about the trajectory of evolution. THere are a lot of dead-ends in the evolutionary paths. I suspect homo sapiens is just one of them.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-12-15 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?