Highlight columns which differer from previous lines
I have text file with numerical data written i开发者_如何学Pythonn lines; there are interspersed input lines (unknowns) and residuals lines (which are supposed to be minimized). I am investigating ways how the iterative solver handles various cases, and would like to highlight every (space-delimited) field in the residuals line which is (textually) different from the same field in the previous residuals line (2 lines above, better given by a regexp). I am free to decorate beginnings of the lines as I like, if that helps.
Is this at all possible with Vim and some regexp magic?
Example file:
input 1 2 3 4 5 6
errors .2 .2 .3 .1 0 0
input 1 2.1 2.9 4 5 6 ## here, 2.1 and 2.9 should be highlighted
errors .21 .3 .44 .3 0 0
input 1 2 3 3.9 5.2 6 ## here, 2, 3, 3.9 and 5.2 should be highlighted
errors .2 .2 .34 .9 1 0
Note: I could code script extracting differences in Python, but I want to have a look at both the actual data and the changes. When it does not work with Vim, I will process it with python and output highlighted HTML, but I will lose automatic folding capabilities.
I think that if you use arrays rather than a regex for the comparison, it might be a lot easier:
<?php
$lastRow=array()
$h=fopen('file','r');
while ($r=fgetcsv($h,0,' ')) // Retrieve a line into an array and split on spaces
{
if (count($lastRow)==0)
{
$lastRow=$r; // Store last line
}
$count++;
if ($r[0]=='input')
{
/*
* this won't find any differences the first run through
*/
$diffs=array_diff($lastRow,$r); // Anything that has changed from the last "input" line is now in $diffs
$lastRow=$r; // Only copy into $lastRow if it's an "input" line
}
/*
* Put your code to output line here with relevant CSS for highlighting
*/
}
fclose($h);
?>
I've not tested this code but I think it's shows how you could get a solution without delving into regex
精彩评论