From 359d4073df24be0bb00bd30cfb4f81a5495a82ec Mon Sep 17 00:00:00 2001
From: Michael Moon <triffid.hunter@gmail.com>
Date: Sun, 11 Dec 2011 08:22:14 +1100
Subject: [PATCH 1/2] add z-every-line script, first attempt

---
 post-processing/z-every-line.pl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 post-processing/z-every-line.pl

diff --git a/post-processing/z-every-line.pl b/post-processing/z-every-line.pl
new file mode 100755
index 000000000..5592c1926
--- /dev/null
+++ b/post-processing/z-every-line.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+
+my $z = 0;
+
+for (<>) {
+	if (/Z(\d+(\.\d+)?)/) {
+		$z = $1;
+		print;
+	}
+	else {
+		if (!/Z/ && /X/ && /Y/ && $z > 0) {
+			s/\s*([\r\n\;\(].*)//gs;
+			print "$_ Z$z $1";
+		}
+		else {
+			print;
+		}
+	}
+}

From ca25494177882982e5ae7a8aacdbd810ec26083d Mon Sep 17 00:00:00 2001
From: Michael Moon <triffid.hunter@gmail.com>
Date: Sun, 11 Dec 2011 08:27:16 +1100
Subject: [PATCH 2/2] add some comments

---
 post-processing/z-every-line.pl | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/post-processing/z-every-line.pl b/post-processing/z-every-line.pl
index 5592c1926..5f9d311b6 100755
--- a/post-processing/z-every-line.pl
+++ b/post-processing/z-every-line.pl
@@ -4,18 +4,20 @@ use strict;
 
 my $z = 0;
 
+# read stdin and any/all files passed as parameters one line at a time
 for (<>) {
-	if (/Z(\d+(\.\d+)?)/) {
-		$z = $1;
-		print;
+	# if we find a Z word, save it
+	$z = $1 if /Z(\d+(\.\d+)?)/;
+
+	# if we don't have Z, but we do have X and Y
+	if (!/Z/ && /X/ && /Y/ && $z > 0) {
+		# chop off the end of the line (incl. comments), saving chopped section in $1
+		s/\s*([\r\n\;\(].*)//s;
+		# print start of line, insert our Z value then re-add the chopped end of line
+		print "$_ Z$z $1";
 	}
 	else {
-		if (!/Z/ && /X/ && /Y/ && $z > 0) {
-			s/\s*([\r\n\;\(].*)//gs;
-			print "$_ Z$z $1";
-		}
-		else {
-			print;
-		}
+		# nothing interesting, print line as-is
+		print;
 	}
 }