Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > eliminating empty elements of a list

Reply
Thread Tools

eliminating empty elements of a list

 
 
Hendrik Maryns
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      01-10-2005
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,"someotherstrin g")
>
>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,
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      01-10-2005
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?
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      01-10-2005
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?"
 
Reply With Quote
 
David Sletten
Guest
Posts: n/a
 
      01-11-2005
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
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      01-11-2005
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Eliminating duplicates entries from a list efficiently Paul Python 9 07-07-2004 04:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57