So I am using a program which spits out a horrible, outdated text/ascii style table as a "Click to Copy" style output. I am wondering how I would go about using RegEx to pull the headings and then the values, and put them into an object. The string in question looks similar to this:
*========================================================================*
|Timestamp ||Key1 |Key2 |Key3 |Key4 |Key5 |Key6 |
*------------------------------------------------------------------------*
| 01/06/2015 12:00:00 ||0 |12 |47.5 |102 |0 |0 |
*========================================================================*
*========================================================*
|Timestamp ||Key1 |Key2 |Key3 |Key4 |
*--------------------------------------------------------*
| 01/06/2015 12:00:00 ||0.8 |120 |475 |1.2 |
*========================================================
I spent a bit of time on it and ended up with one array of Keys, one array of Values for each table, which was then in an array, like
[[KeyArray01], [ValueArray01], [KeyArray02], [ValueArray02]]
After messing around with that for a while, I decided, there has to be a far better solution to this, and I was hoping someone had an example of how to pull the Headings, and Value from this string, and put them straight into an object, something like this:
[{
Timestamp = 01/06/2015 12:00:00
Key1 = 0,
Key2 = 12,
Key3 = 47.5,
Key4 = 102,
Key5 = 0,
Key6 = 0
},{
Timestamp = 01/06/2015 12:00:00
Key1 = 0.8,
Key2 = 120,
Key3 = 475,
Key4 = 1.2
}]
So I can then simply run a look over it, to print out the relevant data needed, in list or table.
I had a bit of a look around, and could not come up with a solution, any help, or a point in the right direction would be greatly appreciated.
Regards Kirt
UPDATE: For thoes asking for the method I used to get the arrays.
function objectify() {
var rawData = codeOutput.getSession().getValue();
b = rawData.match(/\|.*\|/g);
j = [];
for (var i = 0 ; i < b.length; i++) {
j.push(b[i].split("|"))
oid = "";
oval = "";
// Run through each array
// $.each(j,function(i,v) { $.each(v,function(index,value) { console.log(value)});});
} console.log(j)
}
Final Update OK, so all of the answers appear to work on the test data. The one that appears to have worked the best for me is Xotic750 answer. Since it takes into account possible variations in the output, which is perfect for my solution.
Thanks all.
Timestamp: '01/06/2015 12:00:00',Key1: '0',and so on. It may be that CSV is better (greater compatibility with desktop applications).