Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have:

@fruit = qw(oranges apples banannas grapes);

but since I got my wisdom teeth taken out I want to removed apples. Is there a way to completely delete the 2nd element ($fruit[1]) so that everything after it is moved down and the array now reads like this:

@fruit = qw(oranges banannas grapes);

Replies are listed 'Best First'.
Re: shifting the middle of an array?
by lemming (Priest) on Jan 03, 2001 at 03:51 UTC

    Look at splice. It can do pop, push, shift, unshift, and more.

Re: shifting the middle of an array?
by OeufMayo (Curate) on Jan 03, 2001 at 03:59 UTC
    Type perldoc -f splice, and the solution will appear.
    my $bad_fruit = splice(@fruit, 1, 1); print $bad_fruit; # gives "apples"
    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>
Re: shifting the middle of an array?
by repson (Chaplain) on Jan 03, 2001 at 07:00 UTC
    If you didn't know the index of apples then grep would be a good answer.
    @fruit = qw(oranges apples banannas grapes); $remove = 'apples'; @fruit = grep {$_ ne $remove} @fruit;
Re: shifting the middle of an array?
by coreolyn (Parson) on Jan 03, 2001 at 03:55 UTC