http://qs1969.pair.com?node_id=782586


in reply to Deleteing array elements

If you know the index of the element you want to delete you can use a slice
@arr = @arr[0,1,3];
otherwise I'm not sure if you can avoid an (implicit) loop.
One possibility would be a grep
@arr = grep { $_ !~ /c/ } @arr;
Why do you want to avoid a loop? Are there so many elements in the array? Maybe you could avoid putting the unwanted elements in the array in the first place.

cheers, si_lence

Replies are listed 'Best First'.
Re^2: Deleting array elements
by davorg (Chancellor) on Jul 23, 2009 at 08:26 UTC
    @arr = grep { $_ !~ /c/ } @arr;

    That works, of course, with the sample data that we've been given, but in the general case it will remove any element that contains the letter 'c'.

    Regular expressions aren't always the answer. Sometimes a simpler approach is right.

    @arr = grep { $_ ne 'c' } @arr;
    --

    See the Copyright notice on my home node.

    Perl training courses

      Hi davorg, you are - of course - right with your remark that regular expressions aren't always the answer.
      It really depends on the problem. If a special named file should be removed from the array then ne is probably the better solution.
      If all files ending in .log should be removed, then a regex might be the way to go.
      Since there is no information about the general problem in the OP we are left guessing.

      cheers, si_lence