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."
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."
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.