Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Removing empty string elements from an Array

by Spliter (Initiate)
on Nov 13, 2001 at 05:50 UTC ( [id://124970]=perlquestion: print w/replies, xml ) Need Help??

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 - || "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
        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 ''; }



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://124970]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-05-22 20:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found