http://qs321.pair.com?node_id=124970

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

I want to be able to find the empty elements in an array because I know the array I am generating will have empty elements. When I say empty elemts I mean if I print out all the elements in the array at the promt I get this:
e1 e2 <--- this is an empty string element e3 <--- this is another e4
How do I write a loop that will find these empty string elements? Then, how do I remove them from the array once I have found them?

Replies are listed 'Best First'.
Re: Removing empty string elements from an Array
by Masem (Monsignor) on Nov 13, 2001 at 06:01 UTC
    @array = grep { $_ ne '' } @array;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      Taking that one step further, if by "empty" you also mean lines that consist solely of whitespace:
      @array = grep /\S/, @array;

      [ ar0n ]

      Masem wrote:

       @array = grep { $_ ne '' } @array;

      Wow, you can't improve on perfection, very cute. Long-handed its an easy problem, but clumsy because this is succinct and cute...

Re: Removing empty string elements from an Array
by Bobcat (Scribe) on Nov 13, 2001 at 06:24 UTC
    The above suggestion was great. Here's a more readable, and less efficient version:
    my @newArray; foreach my $element ( @array ) { push $element, @newArray if $element; }
    This should push everything you want into @newArray. It's a bit slower than the grep solution, and has additional overhead of using another array; but I find it easier to read.
      Aside from the issue of readability, which is obviously subjective, this solution will drop any occurences of '0' from the array. A more explicit check is necessary, as in the earlier grep solutions:
      my @newArray; foreach my $element ( @array ) { push $element, @newArray if defined $element and $element ne ''; }
      (Personally, I also prefer grep to solve this problem.)
        Forgive a noob, but shouldn't
        push $element, @newArray
        be
        push @newArray, $element
      You call that "more readable"? Are you joking?

      When using any language, go with its strengths. Perl's clarity in brevity is a feature, not a bug.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        Jeez... Somebody hand me a fire extinguisher...

        Yes, I find it more readable. No, I'm not joking. It's about as close to plain English as you can get. It appeared to me that the questioner was not very familiar with some of the interesting nuances that separate perl from C or BASIC.

        When learning a language, it's often usefull to see a "here's what's happening" sort of example -- be it in pseudocode or ineffecient "straight" looping -- to show how a particular operation can happen.

        I'm sorry if my example offended you; I didn't intend to ruin your day.

Re: Removing empty string elements from an Array
by broquaint (Abbot) on Nov 13, 2001 at 15:32 UTC
    Having not seen it listed, here's yet another way to do it
    my @arr1 = ("foo", "", "bar", "", "baz"); my @arr2 = grep $_, @arr1; { $" = ', '; print "@arr1\n@arr2\n"; }
    Since this tests for each element in a boolean context, it will ignore any element perl considers to be false, which includes empty strings (""). However this also has the pitfall of dropping stuff you might want to keep e.g qw(0 1 2) --> (1 2).
    That was the more "perlish" way to do it, but if you're determined to use a loop try this
    my @arr1 = ("foo", "", "bar", "", "baz"); my @arr2; foreach (@arr1) { push @arr2, $_ if $_ ne ''; }

    HTH

    broquaint