I need to iterate over an array and find any entries that match the beginning of entries in a second array and combine that entry with the entry before it. From the cookbook I know how to trim an array down for exact matches (4.7, pg 104), but not for the partials I need to match/move. I've been able to accomplish this with the following code, but I've got to believe there are far more efficient ways of doing this. I've tried to reduce the iterations by purging the moved entries, which helps. But I've been unable to do the same with the search/destroy list. Basically I'm looking for a way to do this more efficiently because as the array sizes grow, I realize the time it takes to do this will go up dramatically.
Thanks.
#!/usr/local/bin/perl -w
use strict;
my @text_remove = ('[TEXT-2]', '[TEXT-4]', '[TEXT-7]');
my @xlate_data = ('[TEXT-1] Data Test ', '[TEXT-2] Second line tacked
+onto #1', '[TEXT-3] Line #3', '[TEXT-4] 4th line goes with #3', '[TEX
+T-5] five-five-FIVE', '[TEXT-6] Five was a Zappa reference...', '[TEX
+T-7] This is a longer entry. But really not much different. I should
+ be part of #6');
my $rm_pos = 0;
foreach my $move (@text_remove) {
print "O: $rm_pos ~ $move\n";
my $xlate_pos = 0;
foreach my $data (@xlate_data) {
if ($data =~ /^\Q$move\E/) {
print "I: $rm_pos ~ $move ~ $data\n\n";
$data =~ s/^\Q$move\E\s*/ /;
$xlate_data[$xlate_pos -1] .= $data;
splice(@xlate_data, $xlate_pos, 1); #remove entry to short
+en loops
}
$xlate_pos++;
# splice(@text_remove, $rm_pos, 1); #remove token entry to shorten
+loops
}
$rm_pos++;
}
foreach (@xlate_data) {
print "NOW: $_\n";
}
-THRAK
www.polarlava.com
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.