Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: how to rm an element from an array?

by atcroft (Abbot)
on Oct 28, 2002 at 03:12 UTC ( [id://208436]=note: print w/replies, xml ) Need Help??

in reply to Re: how to rm an element from an array?
in thread how to rm an element from an array?

Why not a combination of the two solutions, something like:

sub yoink { my ($array_ref, @kill_patterns) = @_; foreach my $kp (@kill_patterns) { @{$array_ref} = grep $_ ne $kp, @{$array_ref}; } }
That way, you could call it with one or more kill patterns, such as &yoink(\@arr, $kp), or &yoink(\@arr, $kp1, $kp2, $kp3), or so forth... (At least, I think it would work....)

Replies are listed 'Best First'.
Re: Re^2: how to rm an element from an array?
by blokhead (Monsignor) on Oct 28, 2002 at 03:52 UTC
    If you always want to modify the original array, you could use function prototypes (let the scolding begin!) and use yoink like a builtin. That way, you wouldn't have to keep passing \@arr as a ref like that. However, if you want to keep the original array intact and let yoink return a separate copy with the proper yoinkage, then this is certainly not at all what you want.
    sub yoink(\@$) { my ($array_ref, $remove) = @_; @$array_ref = grep { $_ ne $remove } @$array_ref; } my @arr = qw/un deux trois quatre cinq six sept huit/; # no passing of \@arr -- oh, so pretty! yoink @arr, 'deux'; print "@arr\n"; __END__ un trois quatre cinq six sept huit


      Slightly different, but I quite like this:

      sub yoink (&\@) { @{$_[1]} = grep !$_[0]->($_), @{$_[1]} } my @arr = qw( aa bb cc ); yoink { $_ eq 'bb' } @arr; print "@arr\n";

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-05-29 17:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found