eliminating empty elements of a list

Discussion in 'Perl Misc' started by Hendrik Maryns, Jan 10, 2005.

  1. Hi,

    I was wondering wether there is an easy way to eliminate all undefined
    or empty elements of a list, except from skipping through it with a
    foreach loop and rebuilding it. So, if I had a list like this:

    (5,"jan","",$whatever,undef,undef,"someotherstring")

    The result should be

    (5,"jan",$whatever,"someotherstring")

    Notice that I want the empty string to be removed too.

    If this is a FAQ, I apologise, and would be very happy with a link.

    Thanks, Hendrik
    Hendrik Maryns, Jan 10, 2005
    #1
    1. Advertising

  2. On Mon, 10 Jan 2005 22:11:59 +0100, Hendrik Maryns
    <> wrote:

    >I was wondering wether there is an easy way to eliminate all undefined
    >or empty elements of a list, except from skipping through it with a
    >foreach loop and rebuilding it. So, if I had a list like this:
    >
    >(5,"jan","",$whatever,undef,undef,"someotherstring")
    >
    >The result should be
    >
    >(5,"jan",$whatever,"someotherstring")


    my @newlist = grep $_, @oldlist;
    # perldoc -f grep


    HTH,
    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jan 10, 2005
    #2
    1. Advertising

  3. Hendrik Maryns

    Anno Siegel Guest

    Jim Gibson <> wrote in comp.lang.perl.misc:

    [...]

    > Keep in mind that this will also eliminate any element with a value 0.
    > If that is not wanted, change to:
    >
    > my @newlist = grep { defined && ($_ ne '') } @oldlist;


    Equivalent, but more common:

    my @newlist = grep { defined && length } @oldlist;

    Anno
    Anno Siegel, Jan 10, 2005
    #3
  4. Anno Siegel schreef:
    > Jim Gibson <> wrote in comp.lang.perl.misc:
    >
    > [...]
    >
    >
    >>Keep in mind that this will also eliminate any element with a value 0.
    >>If that is not wanted, change to:
    >>
    >> my @newlist = grep { defined && ($_ ne '') } @oldlist;

    >
    >
    > Equivalent, but more common:
    >
    > my @newlist = grep { defined && length } @oldlist;
    >
    > Anno


    Thanks all!
    Actually, I solved my problem in a totally different way, making what I
    asked here unnecessary, but thanks anyway. I guess, I'll have to read
    some more tutorials, as I didn't know you could use grep _inside_ Perl.
    You are sure this works on Windows right?

    Hendrik

    PS: What is the difference between c.l.p.misc, c.l.p, c.l.p.moderated
    and c.l.p.tk?
    Hendrik Maryns, Jan 10, 2005
    #4
  5. Hendrik Maryns wrote:

    > Anno Siegel schreef:
    >> Jim Gibson <> wrote in comp.lang.perl.misc:
    >>
    >> [...]
    >>
    >>
    >>>Keep in mind that this will also eliminate any element with a value 0.
    >>>If that is not wanted, change to:
    >>>
    >>> my @newlist = grep { defined && ($_ ne '') } @oldlist;

    >>
    >>
    >> Equivalent, but more common:
    >>
    >> my @newlist = grep { defined && length } @oldlist;
    >>
    >> Anno

    >
    > Thanks all!
    > Actually, I solved my problem in a totally different way, making what I
    > asked here unnecessary, but thanks anyway. I guess, I'll have to read
    > some more tutorials, as I didn't know you could use grep _inside_ Perl.
    > You are sure this works on Windows right?


    Yes. You are not using the Unix utility "grep", you are using the Perl
    function "grep," which is available provided that you're using Perl.
    >
    > Hendrik
    >
    > PS: What is the difference between c.l.p.misc, c.l.p, c.l.p.moderated
    > and c.l.p.tk?


    c.l.p.misc = where you should generally be posting
    c.l.p = dead group, even though your newserver may still be mistakenly
    carrying it. Don't post here.
    c.l.p.moderated = moderated group. Post here if you're willing to submit
    to the moderation. Read the c.l.p.moderated FAQ before trying this group.
    c.l.p.tk = for questions about Perl/Tk, which is Perl with GUI widgets
    added on for your point-n-click pleasure.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Jan 10, 2005
    #5
  6. Hendrik Maryns wrote:

    > Hi,
    >
    > I was wondering wether there is an easy way to eliminate all undefined
    > or empty elements of a list, except from skipping through it with a
    > foreach loop and rebuilding it. So, if I had a list like this:
    >
    > (5,"jan","",$whatever,undef,undef,"someotherstring")
    >
    > The result should be
    >
    > (5,"jan",$whatever,"someotherstring")
    >
    > Notice that I want the empty string to be removed too.
    >
    > If this is a FAQ, I apologise, and would be very happy with a link.
    >
    > Thanks, Hendrik


    This will remove all of the undef elements:
    @out = grep { defined($_) } @in;

    Or if you want to be chintzy:
    @out = grep { defined } @in;

    However, that leaves empty strings in. On the other hand, this gets rid
    of undef and empty strings:
    @out = grep { $_ } @in;

    But it also removes 0's.

    So this should work for your purposes:
    @out = grep { $_ || $_ eq '0' } @in;

    (Of course, if the value of $whatever is undef or an empty string it too
    will be removed.)

    David Sletten
    David Sletten, Jan 11, 2005
    #6
  7. On Mon, 10 Jan 2005 13:46:23 -0800, Jim Gibson
    <> wrote:

    >> my @newlist = grep $_, @oldlist;

    >
    >Keep in mind that this will also eliminate any element with a value 0.


    You're right. OTOH I mainly wanted to just point the OP to grep(). And
    after all it may also be what he really wanted, even if I must admit I
    doubt so...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jan 11, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Guy
    Replies:
    3
    Views:
    597
    Paul Guy
    May 27, 2004
  2. Lukas
    Replies:
    3
    Views:
    783
    spiff
    Nov 10, 2005
  3. Paul
    Replies:
    9
    Views:
    456
    Paul Rubin
    Jul 7, 2004
  4. Adam Hartshorne
    Replies:
    2
    Views:
    364
    Nitin Motgi
    Jan 27, 2006
  5. Tzury Bar Yochay
    Replies:
    1
    Views:
    391
    Gabriel Genellina
    Mar 24, 2008
Loading...

Share This Page