Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the quikest way to remove empty array elements 1

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I know how to remove empty array elements using a loop and check each separately. I wonder if there is a quicker way using regex or something else?

Regards,

Ron
 
well, you could use grep or map, but ultimately, they're just foreach loops themselves. however, in the spirit of making things look pretty, how 'bout this:[tt]
@full = map {$_ ? $_ : ()} @partial;
[/tt]
or this:[tt]
@full = grep {$_} @partial;
[/tt] "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Thanks again. It is just like you said, it looks nicer and it takes less code!

Regards,

Ron
 
I tried to figure out how both options work. But all these '//~? : /g' expressions are still not making any sense to me.

The second options seems not to difficult to understand as apparantly 'grep' evaluates each element and does something if true.

so {$_} evaluates if the element is not empty if true include it in @partial!

But I can't figure out how the expression for map works?

Ron
 
okay, i'll show you what's going on.
@partial is to be the array that has empty spaces in it. both map and grep are to be working on it's values. sorry if the naming sequence didn't lend itself intuitively. first, i'll explain grep.
grep looks at each element of an array, one at a time, and evaluates the block of code (the stuff inbetween the '{}' ). it set's the element it is looking at to $_ before it evaluates the code, so '$_' is the way to access the array element inside the block. then, after the block is done, if it returns a true value, grep pushes that element onto an array, the array it eventually returns. since empty elements evaluate to false, they won't be included in the final array. downside is that elements that are zero will be excluded as well, even though they aren't quite empty. all the block is in this instance is just '$_', which means if '$_' is true (non-zero, with a value) it will be included in the final array (which is then assigned to @full).
map works a little the same. what it does is it evaluates the block for every element, but this time makes an array out of the return values of that block. the block says this:[tt]
$_ ? $_ : ()[/tt]
this is basically an if/else statement. first it evaluates the expression in front of the '?'. if this is a true statement, it then evaluates and returns the expression inbetween the '?' and the ':'. if the first expression is false, it evaluates and returns the last part (inbetween the ':' and the inplied ';').
so, it looks at $_. if it's true, it pushes it's value onto the array. if it's false, it pushes on a '()', which, when put in an array, dissappears.

if you want it to include elements whose value is zero, make the grep block look like this:[tt]
{($_ == 0) || ($_)}[/tt]
or the map block look like this:[tt]
{(($_==0)||($_)) ? $_ : ()}[/tt]
that way it checks to make sure that the element isn't zero first, then checks to see if it's empty.

HTH "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top