views:

43

answers:

2

I have a long string with Headers and Sub-headers. Every header is supposed to have a sub header, but the string pre-processing, does not. I need to manipulate it in such a way that every header has a Sub-header.

Every header that does not have a string has a reference point under different header. The header missing the subheader needs to grab the subheader from the reference's immediate parent/proceeding header.

Here's what it looks like:

Header 1
subheader - somedata A
text
reference-header-3
stuff in the way

Header 2
subheader - somedata B
stuff in the way
stuff in the way

Header 3
stuff in the way
stuff in the way
reference-header-5

Header 4
subheader - somedata C
some text

Header 5
more text

I need to get it to be like this:

Header 1
subheader - somedata A
text
reference-header-3
stuff in the way

Header 2
subheader - somedata B
stuff in the way
stuff in the way

Header 3
subheader - somedata A [this is copied from header 1]
stuff in the way
stuff in the way
reference-header-5

Header 4
subheader - somedata C
some text

Header 5
subheader - somedata A [this is copied from header 3]
more text

If anyone know of any string libraries that can help do this that would be awesome. I don't know how to go about this, I'm thinking of converting them to DOM elements so I can traverse them with jQuery, and then convert back. But that sounds a little icky.

Anyone know how to do this?

Thanks in advance.

+1  A: 

If you have control over the backend, you should have this data sent over as JSON. That would be the best bet. It is also relatively painless. You also shouldn't be using jQuery for this (its use is rather irrelevant here) unless you were using AJAX to fetch JSON like I described.

Or you can do something like this:

var str = "Header 1\none\ntwo\nthree\n\nHeader 2\nsubheader - one\ntwo\nthree\n\nHeader 3\none\ntwo\nthree\n";
var lines = str.split(/\n/);

var headerMap = {};
var currentHeader = "";

for(var i = 0; i < lines.length; i++) {

   var line = lines[i];
   line = line.replace(/^\s+/, "").replace(/\s+$/, ""); //trim whitespace

   if(/^Header [0-9]+$/.test(line) && line != currentHeader) {
      headerMap[line] = new Array();
      currentHeader = line;
   }

   //don't add blank lines
   if(line != "") {
      headerMap[currentHeader][headerMap[currentHeader].length] = line;
   }
}

Now you have a map that is keyed by Header 1 and Header 2 and so on. The value for each key is an array that contains the various subheaders. You can easily iterate over these values and check the very first value in the array to see if it has the subheader - prefix. If not, you can add it.

I just noticed the second part. I guess what you can do after you do the above is make a second pass and parse out the reference parts and insert the appropriate values there. Slak's solution might be a one-pass one I think (from the quick read I gave it). OR you can add an else-if to the above code (within the loop where it checks for the hedaer) to check and see if it matches your reference directive. If it does, grab the existing reference and add it what you have (this only works for backward references). If you have forward references then you will need to have a second pass.

Vivin Paliath
A: 

You should loop through the lines in the string and build a lookup table mapping header references to their data fields, and add each line to an output array.

When you encounter a header, set a flag to indicate that you're expecting a subheader.
If the flag is set and the current line isn't a subheader, look up the reference in the lookup table and add a subheader line to the output array before the actual line.

If the last line might be a header, you'll need to repeat that check after the loop.

If the referencing line might appear after the header, you'll need to check for & add subheaders in a second, backwards, loop through output after filling output and populating the lookup table. If so, you should insert the subheaders by calling splice.

When you're finished, call output.join('\n').

SLaks
Thank you, makes a lot of sense. I'm thinking about it currently. If there is any code you can share that would be great too.
Mark