in reply to Reducing Array Interations
Sorry, I golfed it a bit....my $patt = join('|', map {"(\Q$_\E)"} @text_remove); my @tmp; s/^$patt//o ? $tmp[-1] .= $_ : push(@tmp,$_) for @xlate_data; @xlate_data = @tmp;
Update: Ok, slight explanation....
We generate a pattern that will match all (any??) of the elements in @text_remove. In general, this is better than looping over each pattern individually. For one thing, it allows you to use the /o modifier to avoid recompiling the regex over and over again.
We then loop through the @xlate_data array looking for this pattern. If we find it, we lop off whatever matched and glue whats left onto the last field in our temporary array (thats what $tmp[-1] .= $_ does). On the other hand, without a match we simple push the value onto the temporary array.
Finally we overwrite @xlate_data with the @tmp array. This probably isn't how I would have coded it, but since the answer overwrites the data in the original script, I did it here as well. (I also already munged @xlate_data inplace, since I knew I was going to trash it anyway)
-Blake
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Reducing Array Interations
by THRAK (Monk) on Aug 16, 2001 at 17:45 UTC | |
by blakem (Monsignor) on Aug 17, 2001 at 02:03 UTC | |
by THRAK (Monk) on Aug 17, 2001 at 15:53 UTC |