For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > Remove whitespace elements of an array.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Remove whitespace elements of an array.
fenisol3

2004-03-30, 5:39 pm

Hello,

What I am trying to do is to go through each element in an array using
foreach loop, delete the elements that are empty (blank spaces), and store
the rest in another array. For some reason, (@array = grep /\S/, @array)
doesn't do anything to the array.
If you know how to do this, please help me. Thanks.
-Fenisol3


Gunnar Hjalmarsson

2004-03-30, 5:39 pm

[ This relates to a previous thread with a similar name. You should
have posted a follow-up to that thread instead of starting a new one. ]

fenisol3 wrote:
> What I am trying to do is to go through each element in an array
> using foreach loop, delete the elements that are empty (blank
> spaces), and store the rest in another array. For some reason,
> (@array = grep /\S/, @array) doesn't do anything to the array.


Yes, it removes possible whitespace elements in the array.

If you claim otherwise, please post a short but complete program that
people can copy and run, and that made you come to your conclusion.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Paul Lalli

2004-03-30, 5:39 pm

On Tue, 30 Mar 2004, fenisol3 wrote:

> Hello,
>
> What I am trying to do is to go through each element in an array using
> foreach loop, delete the elements that are empty (blank spaces), and store
> the rest in another array. For some reason, (@array = grep /\S/, @array)
> doesn't do anything to the array.
> If you know how to do this, please help me. Thanks.
> -Fenisol3
>


Please do not start a new thread instead of continuing the old one.

I assure you, that line does indeed remove white-space-only elements from
an array. Therefore, either there is something else wrong, or you are not
adequetley describing your problem or data. Please post a *short but
complete* program showing us what's going wrong.

Paul Lalli
Tore Aursand

2004-03-30, 5:39 pm

On Tue, 30 Mar 2004 16:45:39 -0500, fenisol3 wrote:
> What I am trying to do is to go through each element in an array using
> foreach loop, delete the elements that are empty (blank spaces), and store
> the rest in another array. For some reason, (@array = grep /\S/, @array)
> doesn't do anything to the array.


You still don't tell us anything about what you've tried so far. What
doesn't work? What have you tried?

The simples approach - at least for me - would be something like this:

my @old = ( 'a', 'b', '', 'c', ' ', 'd' );
my @new;

foreach ( @old ) {
if ( /\S/ ) {
push( @new, $_ );
}
}

This can easily have been cut down to:

foreach ( @old ) {
push( @new, $_ ) if ( /\S/ );
}

....and again to:

my @new = grep /\S/, @old;

If none of these suggestions work, you're not describing your problem
sufficiently. Please do that next time you post here. Give us some
example data and what you have tried so far (which doesn't work).


--
Tore Aursand <tore@aursand.no>
"I know not with what weapons World War 3 will be fought, but World War
4 will be fought with sticks and stones." -- Albert Einstein
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com