Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Deleteing array elements

by Sun751 (Beadle)
on Jul 23, 2009 at 07:04 UTC ( [id://782582] : perlquestion . print w/replies, xml ) Need Help??

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

I am bit lost and want to know, if I have an array @a with list of file name and if suppose I want to delete one of the file name out of that array is there any short cut code rather than looping to all array, for example,
@a = ['a','b','c','d'];
how can I delete 'c' out of it without looping??? Any Suggestion? Please, Cheers

Replies are listed 'Best First'.
Re: Deleteing array elements
by Corion (Patriarch) on Jul 23, 2009 at 07:08 UTC
    @a = ['a','b','c','d'];

    That's likely not "an array" in the sense you want. That's just an array with a single element that is a reference to another array with four elements. More likely, you want an array of four elements for a start:

    @a = ('a','b','c','d');

    Then you want to read splice and then use it.

      Once you've got your array set up as described by Corion, use splice
      my @a=('a','b','c','d'); splice @a, 2, 1; print join ",", @a;
      Linux, perl, punk rock, cider:
        I prefer to write splice @a, 2, 1, (); it doesn't do anything different, but it makes things clear as well as making things parallel with substr (substr and splice act differently with three arguments, but they act the same with four). Am I the only one?
Re: Deleteing array elements
by FunkyMonk (Chancellor) on Jul 23, 2009 at 07:16 UTC
    You probably haven't got the data structure you think you have. What I think you mean is:
    my @a = ('a', 'b', 'c', 'd'); #or my @a = qw( a b c d );

    To delete the array element containing 'c', see grep:

    @a = grep { $_ ne 'c' } @a;

    Update Further to Corion's (and others) advice

    • If you know the index of the element you want to delete, use splice
    • If you know the contents of the element you want to delete, use grep
Re: Deleteing array elements
by si_lence (Deacon) on Jul 23, 2009 at 07:19 UTC
    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

      @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