Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

(dkubb) Re: (2) What's the best metaphor for deleting something from a list?

by dkubb (Deacon)
on Aug 10, 2001 at 06:24 UTC ( [id://103744]=note: print w/replies, xml ) Need Help??


in reply to What's the best metaphor for deleting something from a list?

There is no need to increment a counter to keep track of how many elements are deleted from the array. You can simply get the size before the deletion, and substract the size afterwards to get the difference.

Here's how I would do it:

sub delete_field { my $self = shift; my $delete_field = shift; my $before_deletion = @{$self->{_fields}}; @{$self->{_fields}} = grep { $delete_field ne $_ } @{$self->{_fields}}; return $before_deletion - @{$self->{_fields}}; }

You may also want to check out Removing certain elements from an array, which has some neat idioms somewhat related.

  • Comment on (dkubb) Re: (2) What's the best metaphor for deleting something from a list?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://103744]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found