Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to remove the certain element of an object

by Athanasius (Monsignor)
on Feb 04, 2013 at 04:08 UTC ( #1016892=note: print w/ replies, xml ) Need Help??


in reply to How to remove the certain element of an object

++tobyink for his excellent solution. And note that the logic for removing people from the population should not appear in main, but should be encapsulated in the container class:

#! perl use Modern::Perl; package Person { use Moose; use constant DURATION => 100; has use_duration => ( isa => 'Int', is => 'ro', default => 1, ); has frequency => ( isa => 'Int', is => 'ro', default => 50, ); has number => ( isa => 'Int', is => 'rw', default => 0, ); sub need_to_go { my ($self) = @_; return rand(DURATION) + 1 <= $self->frequency; } } package Nation { use Moose; has population => ( isa => 'ArrayRef[Person]', is => 'ro', traits => ['Array'], handles => { obtain => 'push', }, ); sub cull { use List::MoreUtils qw(part); my ($self) = @_; my ($keep, $go) = part { $_->need_to_go } @{ $self->population + }; @{ $self->population } = @$keep; } sub display { use Data::Dump; my ($self, $msg) = @_; say "\n$msg\n"; dd $self; } } package main { my $people = Nation->new; my $people_nr = 25; for my $case (0 .. $people_nr) { my $person = Person->new; $person->number($case); $people->obtain($person); } $people->display('Before:'); $people->cull; $people->display('After:'); }

Typical output:

14:06 >perl 516_SoPW.pl Before: bless({ population => [ bless({ frequency => 50, number => 0, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 1, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 2, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 3, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 4, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 5, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 6, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 7, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 8, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 9, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 10, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 11, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 12, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 13, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 14, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 15, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 16, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 17, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 18, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 19, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 20, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 21, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 22, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 23, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 24, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 25, use_duration => 1 }, "Perso +n"), ], }, "Nation") After: bless({ population => [ bless({ frequency => 50, number => 0, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 1, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 2, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 5, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 6, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 9, use_duration => 1 }, "Person +"), bless({ frequency => 50, number => 12, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 13, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 14, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 15, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 16, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 18, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 21, use_duration => 1 }, "Perso +n"), bless({ frequency => 50, number => 22, use_duration => 1 }, "Perso +n"), ], }, "Nation") 14:06 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: How to remove the certain element of an object
Select or Download Code
Re^2: How to remove the certain element of an object
by vagabonding electron (Hermit) on Feb 04, 2013 at 10:13 UTC
Re^2: How to remove the certain element of an object # Seems to work now!
by vagabonding electron (Hermit) on Feb 04, 2013 at 15:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016892]
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-07-30 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls