++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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.