mdunnbass has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

This is probably a lot simpler than I am thinking it is, but I figured I'd ask. My brain's not been at full speed for days now, and so I'm having a hard time logic-ing through this

What I'm trying to do:
I have a series of text blocks, stored as array elements in an HoA. I am scanning the text elements for regex pattern matches, and formatting the matching patterns with <span> tags. Because the patterns are user-defined, I don't know if any of them are going to overlap, and so I want to toss in a brief check to see if the matches overlap, and if so, do something about it.

How I thought I'd do it:
My first thought is to scan the text, and for each <span> increment an index value, and for each </span>, decrement it. If at any point the index reaches a number above 1, perform an action. Does that make sense to people?

Second, the actions I'm doing with the span tags is basically just text-highlighting. My thoughts then are, if there are no open span tags (i.e., index = 0), highlight with specified color. If there are open span tags, than close the previous span tag prematurely, open a new one, and make the text color of the overlap text the color of the highlighting from the last span tag, and the highlighting of this text that specified for the current span. At the location of the previous span's original endpoint, change the text back to black, and keep the highlighting.

Of course, I need to be prepared for the possibility of a third overlapping pattern, but I am not sure how to work that. I am also not sure of what to do if 2 different matches start at the same location in the text. Well, I know what I want to do - make the text color match 1 pattern and the highlight color match the other, but I am not sure how to do it.

As I said, this probably isn't very hard to do, but my brain hurts.

If someone could please just toss together a quick outline of code, I would gladly and very thankfully finesse it to make it work the way I want, I'm just not thinking clearly enough to figure out how to start.

Thanks,
Matt

Replies are listed 'Best First'.
Re: Keeping track of html tags
by j3 (Friar) on Nov 21, 2006 at 22:08 UTC

    Just curious, Matt, why is your text broken up into pieces scattered across a HoA? Is it too large to condense down one big string for processing? Putting it all into one string might make your task easier.

    Also, you might provide a sample of what you're trying to parse, to give folks trying to help out a leg up.

      Sorry about that.

      What I'm doing is searching a file containing the genome of an organism for chunks of DNA sequence containing user-input snippets of DNA (multiple 10 base long binding sites). The %HoA consists of (for the keys) the name of the chromosome the match was found on, and the value of the hash is arrays where each element is the sequence of the matching pattern found on that chromosome.

      What I'm trying to highlight is all the occurances of the 10 base snippets of user input DNA.

      So, the text is scattered because each chunk of text corresponds to a different match to the search criteria.

      THanks for your question,
      Matt

Re: Keeping track of html tags
by robot_tourist (Hermit) on Nov 22, 2006 at 16:11 UTC

    If you add a class attribute to your span tags, with a class for each layer, would CSS then solve your highlighting problem for you?

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
    Robot Tourist, by Ten Benson

Re: Keeping track of html tags
by j3 (Friar) on Nov 24, 2006 at 21:24 UTC

    Matt, it looks like you've got a pretty good idea for a technique to use. My advice to you is to make up some sample data to parse (to populate a small HoA), and then take a whack at implementing that algorithm you have in mind. If this small sample program doesn't work as you expect, and you've tried figuring it out but are then still stuck, you can then post what you've got at that point and you'll likely get some help.

    Also remember, the more you can shave down your sample code to be a *minimal* but clear and complete (though broken) program, the easier it will be for some monk to come along and point out the corrections you might make.

    Good luck! Sounds like you've got some fun stuff to work on! :)