eliminating empty elements of a list

H

Hendrik Maryns

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
 
M

Michele Dondi

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
 
A

Anno Siegel

[...]
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
 
H

Hendrik Maryns

Anno Siegel schreef:
[...]

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?
 
C

Chris Mattern

Hendrik said:
Anno Siegel schreef:
[...]

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?"
 
D

David Sletten

Hendrik said:
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
 
M

Michele Dondi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top