Gossamer Forum
Home : General : Perl Programming :

trimming an array

Quote Reply
trimming an array
I got an array with some empty elements inside..

@a = ("a","b","","","c","","");

how to change it to

@a=("a","b","c");

thanks
Quote Reply
Re: [golden_water] trimming an array In reply to
Long winded way...

Code:
my @a = ("a","b","","","c","","");
my @b;

foreach (@a) {
if ($_) { push(@b,$_); }
}

@a = @b;

Not sure if thereis a better/faster way.. but thats probably how I would do it (gotta admit, I've never had to do it before).

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [golden_water] trimming an array In reply to
There's a few ways. One pretty simple one:

@a = grep {/./} @a;

--mark
Quote Reply
Re: [Mark Badolato] trimming an array In reply to
very simple question needs a simple answer..

thank you and this is what I need.........