in reply to Array Cleaning
No one has yet explained why this doesn't work as you expect.
Consider an array of three values:
my @to_clean = ( '', 1, 2 );We'll loop through the array with indices. We'll make sure that the element at that position is defined, true, and not the empty string:
for my $i ( 0 .. $#to_clean ) { next if defined $to_clean[ $i ] and $to_clean[ $i ]; next unless $to_clean[ $i ] eq ''; }
Here's where it gets tricky. By spliceing, you're changing the original array.
splice( @to_clean, $i, 1 );After the first loop, we'll splice out the empty string at index zero. The array will then contain 1 and 0 2. Unfortunately, $i will be 1 on the next loop iteration, and it won't check the 0th element again.
You could mess with redo. You could push the elements you want to keep on a secondary array. It's unlikely you'll come up with anything simpler or faster than grep though.
Update: This lets undef through, but it catches zero. It's just an example, though.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Array Cleaning
by parv (Parson) on May 03, 2003 at 06:42 UTC |