Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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

by blokhead (Monsignor)
on Oct 28, 2002 at 03:52 UTC ( [id://208439]=note: print w/replies, xml ) Need Help??

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

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


Replies are listed 'Best First'.
Re^4: how to rm an element from an array?
by tobyink (Canon) on Feb 27, 2020 at 23:30 UTC

    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://208439]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-05-28 04:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found