Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

++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,


In reply to Re: How to remove the certain element of an object by Athanasius
in thread How to remove the certain element of an object by vagabonding electron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2024-04-18 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found