Hmm ... if you know that the key parts of your data are at the beginning of each element, and that every line begins with some sort of tag along the lines your sample data has, the following trick might work reasonably well (provided you have a short list of keys to remove). No claims about efficiency are made off the top of my head, but you'll iterate through the array only once with this:

# build a regex to match just your keys my $removers = "^(?:" . join ("|", map { quotemeta $_ } @text_remove) +. ") "; # print $removers out if you're not getting what you expect # strip the patterns from the beginning of each line. @xlate_data = map { s/$remove//; $_; } @xlate_data;

OK, that's a start ... the funky bits have been *removed* from the start of each line that matches. Now, how to pack the array up? I don't like the idea of modifying the array in place; so why don't we just join the elements of the array into a string and use a regex on that string to remove the delimiter in front of an element that *doesn't* begin with a "[" character?

# use whatever delimiter makes sense here. I'm assuming # you don't have newlines at the ends of the elements already. my $full_string = join "\n", @xlate_data; # since you know all the [ are gone from # the elements that matched, and that they will be present # in the ones that didn't match, a newline followed by # something other than a [ is a line you want to join. # so strip \n's not followed by a [ $full_string =~ s/\n([^[])/$1/g; # back to an array @xlate_data = split/\n/, $full_string;

Yeah, that seems kinda wacky to me too =) But something like that might do the trick, as long as your data is well-behaved.

HTH!

<code> perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); $rose = "smells sweet to degree $n"; *other_name = *rose; print "$other_name\n"' </code

In reply to Re: Reducing Array Interations by arturo
in thread Reducing Array Interations by THRAK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.