Dear Monks

if I will to delete the certain element of an array I could do the following:

#!/usr/bin/perl use strict; use warnings; use List::MoreUtils qw(first_index); my @array = ( 10, 15, 20, 25, 30, 20 ); my $test = 25; purge_this_one( \@array, $test ); print "@array\n"; sub purge_this_one { my $idx = first_index { $_ == $_[1] } @{$_[0]}; return @{$_[0]} if $idx == -1; # added (s.Update) splice( @{$_[0]}, $idx, 1 ); return @{$_[0]}; }

Now I try to perform the same thing in an object whose elements are another objects. As a very simple-level Perl programmer I failed miserably and I would be very grateful for your help.

Here is the code (minimal working example) with comments in the problem part:

#!/usr/bin/perl use strict; use warnings; 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 population => ( handles => {obtain => 'push', release => 'shift', inventory => 'count', find => 'first_index', purge => 'delete'}, isa => 'ArrayRef[Person]', default => sub{ [] }, traits => ['Array'], is => 'ro', ); has number => ( isa => 'Int', is => 'rw', default => 0); sub need_to_go { my $self = shift; return ( rand(DURATION)+1 <= $self->frequency) ? 1 : 0; } no Moose; package main; use Data::Dumper; my $people_nr = 25; my $people = Person->new; for my $case ( 1 .. $people_nr ) { my $per = Person->new; $per->number( $case ); $people->obtain( $per ); } for my $p ( @{ $people->population } ) { if ($p->need_to_go) { # My problem is here. # I suppose to use # first_index( sub { ... }) method # and then delete or splice # but I cannot figure out what to feed # the sub { ... } with } } print Dumper $people;

Thank you in advance!

VE

Update: Improved the subroutine purge_this_one by adding the line  return @{$_[0]} if $idx == -1; after comment from locked_user sundialsvc4

In reply to 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.