{"id":2133,"date":"2013-04-18T21:51:26","date_gmt":"2013-04-18T09:51:26","guid":{"rendered":"http:\/\/mhjb.co.nz\/blog\/?p=2133"},"modified":"2013-04-18T21:51:26","modified_gmt":"2013-04-18T09:51:26","slug":"ruby-code-to-combine-ranges-in-a-book-index","status":"publish","type":"post","link":"http:\/\/mhjb.co.nz\/blog\/archives\/2133","title":{"rendered":"Ruby code to combine ranges in a book index"},"content":{"rendered":"<style> #wrap_githubgist5411520 .gist-data {max-height: 100%;} <\/style><div id=\"wrap_githubgist5411520\" style=\"width:100%\"><script>document.write('<link rel=\"stylesheet\" href=\"https:\/\/assets-cdn.github.com\/assets\/gist\/embed-926c753a79847cfe2c18ef7826ac3dc4b2c160e1e8bdc5d2b14384d8cc9eedaa.css\">')\ndocument.write('<div id=\\\"gist5411520\\\" class=\\\"gist\\\">\\n    <div class=\\\"gist-file\\\">\\n      <div class=\\\"gist-data\\\">\\n        <div class=\\\"js-gist-file-update-container js-task-list-container file-box\\\">\\n  <div id=\\\"file-gistfile1-txt\\\" class=\\\"file\\\">\\n    \\n\\n  <div class=\\\"blob-wrapper data type-text\\\">\\n      <table class=\\\"highlight tab-size js-file-line-container\\\" data-tab-size=\\\"8\\\">\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L1\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"1\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC1\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">def combine_nums(line)<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L2\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"2\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC2\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tline.gsub!(\/(\\\\d+), (?=(\\\\d+))\/) {  \t\t\t\t\t\t\t# combine consecutive &amp; identical numbers<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L3\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"3\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC3\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\tif $2.to_i - $1.to_i == 1\t\t\t\t\t\t\t\t# (looks-ahead so as to check<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L4\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"4\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC4\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\t\t&quot;#{$1}-&quot;\t\t\t\t\t\t\t\t\t\t\t# every set of two numbers)<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L5\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"5\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC5\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\telsif $2.to_i == $1.to_i<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L6\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"6\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC6\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\t\t&quot;&quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L7\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"7\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC7\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\telse<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L8\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"8\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC8\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\t\t&quot;#{$1}, &quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L9\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"9\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC9\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\tend<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L10\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"10\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC10\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t}<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L11\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"11\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC11\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tline.gsub!(\/-(\\\\d+), (\\\\d+)\/) {   \t\t\t\t\t\t\t# remove numbers after ranges included in same<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L12\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"12\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC12\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\tif $2.to_i &lt; $1.to_i<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L13\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"13\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC13\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\t\t&quot;-#{$1}&quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L14\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"14\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC14\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\telse<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L15\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"15\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC15\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\t\t&quot;#{$&amp;}&quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L16\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"16\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC16\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\tend<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L17\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"17\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC17\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t}<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L18\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"18\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC18\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tline.gsub!(\/(\\\\d+)-(\\\\d+-)+(\\\\d+)\/)\t{ &quot;#{$1}-#{$3}&quot; }  \t\t# remove inner numbers from ranges<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L19\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"19\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC19\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tline.gsub!(\/(\\\\d)(\\\\d\\\\d)-\\\\1(\\\\d\\\\d)\/)\t{ &quot;#{$1}#{$2}-#{$3}&quot;}\t# make e.g. 323-343 into 323-43<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L20\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"20\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC20\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tline.gsub!(\/(\\\\d0\\\\d)-0(\\\\d)\/) \t\t{ &quot;#{$1}-#{$2}&quot;}\t\t# make e.g. 306-08 into 306-8<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L21\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"21\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC21\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\\n<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L22\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"22\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC22\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\treturn line<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L23\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"23\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC23\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">end<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L24\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"24\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC24\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\\n<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L25\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"25\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC25\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">if ARGV.empty?<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L26\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"26\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC26\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tputs &quot;Please supply a filename, or --clipboard&quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L27\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"27\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC27\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">elsif ARGV[0] == &quot;--clipboard&quot;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L28\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"28\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC28\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\trequire &#39;clipboard&#39;<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L29\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"29\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC29\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tclipout = []<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L30\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"30\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC30\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tClipboard.paste.split(&quot;\\\\r&quot;).each { |line| clipout &lt;&lt; combine_nums(line) }<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L31\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"31\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC31\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tClipboard.copy(clipout.join(&quot;\\\\r&quot;))<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L32\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"32\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC32\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">else<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L33\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"33\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC33\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tFile.open(ARGV[0]).each(sep=&quot;\\\\r&quot;) do |line|\t\t\t\t\t\t# Mac classic newlines<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L34\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"34\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC34\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\t\tputs combine_nums(line)<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L35\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"35\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC35\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">\tend<\\\/td>\\n      <\\\/tr>\\n      <tr>\\n        <td id=\\\"file-gistfile1-txt-L36\\\" class=\\\"blob-num js-line-number\\\" data-line-number=\\\"36\\\"><\\\/td>\\n        <td id=\\\"file-gistfile1-txt-LC36\\\" class=\\\"blob-code blob-code-inner js-file-line\\\">end<\\\/td>\\n      <\\\/tr>\\n<\\\/table>\\n\\n  <\\\/div>\\n\\n  <\\\/div>\\n  \\n<\\\/div>\\n\\n      <\\\/div>\\n      <div class=\\\"gist-meta\\\">\\n        <a href=\\\"https:\/\/gist.github.com\/mhjb\/5411520\/raw\/0ed528938ddc5d6102c6143da17ae283ee8db4aa\/gistfile1.txt\\\" style=\\\"float:right\\\">view raw<\\\/a>\\n        <a href=\\\"https:\/\/gist.github.com\/mhjb\/5411520#file-gistfile1-txt\\\">gistfile1.txt<\\\/a>\\n        hosted with &#10084; by <a href=\\\"https:\/\/github.com\\\">GitHub<\\\/a>\\n      <\\\/div>\\n    <\\\/div>\\n<\\\/div>\\n')\n<\/script><div style='margin-bottom:1em;padding:0;'><noscript><code><pre style='overflow:auto;margin:0;padding:0;border:1px solid #DDD;'>Not Found<\/pre><\/code><\/noscript><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2133","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=2133"}],"version-history":[{"count":5,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions"}],"predecessor-version":[{"id":2138,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions\/2138"}],"wp:attachment":[{"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=2133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=2133"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mhjb.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=2133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}