in reply to Reducing Array Interations

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

Replies are listed 'Best First'.
Re: Re: Reducing Array Interations
by THRAK (Monk) on Aug 16, 2001 at 17:48 UTC
    arturo, I unfortunately have some other things I need to work on now, but I will ponder your input further and let you know if I find anything interesting/useful. Thank you.

    -THRAK
    www.polarlava.com