{"id":975,"date":"2010-06-30T07:58:17","date_gmt":"2010-06-30T14:58:17","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=975"},"modified":"2016-07-14T14:52:32","modified_gmt":"2016-07-14T18:52:32","slug":"parsing-columns-like-a-ninja","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/","title":{"rendered":"Parsing Columns Like A Ninja"},"content":{"rendered":"<p>How many times have you written this code? :<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:c825b008-c502-408c-a841-f8487ab26076\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/16b38a5323d00b80001aa5004896f506.js\"><\/script><\/p>\n<\/div>\n<p>Or some code like it.\u00a0 It is pretty common to parse a line and then take each column and store it in your object as data.<\/p>\n<p>One of the annoying problems is that if you have optional columns you have to check to see if they are there before you can parse them.<\/p>\n<p>You\u2019re also repeating your code to strip the quotes off, or whatever other preprocessing you are doing, all over the place.<\/p>\n<p>I know you can use a data driven approach to specify column to property mappings, but I wanted a really low tech, simple solution.<\/p>\n<p>I finally came up with one using one of my favorite C# constructs.<\/p>\n<h2>Action&lt;&gt; is superb for solving these kinds of problems<\/h2>\n<p>See if this code makes you feel any better:<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:caa076af-a3dd-444d-a5ca-934a35961566\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/c948016eab0f06c41baa4169196cacaf.js\"><\/script><\/p>\n<\/div>\n<p>It may not seem like much.\u00a0 It is not really a reduction in code, but we have done a few important things here.<\/p>\n<ul>\n<li>Removed the explicit handling of optional columns, since we are now only populating columns that exist.\u00a0 (Adding a new optional column is as easy as adding one more line to the list.)<\/li>\n<li>Removed the responsibility from the code of explicitly tracking the column numbers.\u00a0 Column number mapping now is implicit by the order of the columns in the list.<\/li>\n<li>Removed the hidden code duplication of having calls to StripQuotes repeated for each column.<\/li>\n<li>Separated the mapping of properties to columns from the assignment of them.<\/li>\n<\/ul>\n<p>That last point deserves a little more explanation.\u00a0 Why do we care if we have separated the mapping of properties to columns from the assignment?<\/p>\n<p>The answer is not obvious until you try and use this same code to handle a different set of columns, or columns in a different order.<\/p>\n<p>By separating out the mapping, we can pass the assignment code a different set of mappings, and it will still work.<\/p>\n<p>This allows us to reuse the logic we have in the assignment of the columns to properties instead of rewriting it for other column to property mappings or orderings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times have you written this code? : Or some code like it.\u00a0 It is pretty common to parse a line and then take each column and store it in your object as data. One of the annoying problems is that if you have optional columns you have to check to see if they&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3378,2185,2426,148],"tags":[],"class_list":["post-975","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-best-practices","category-c","category-design"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Parsing Columns Like A Ninja<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parsing Columns Like A Ninja\" \/>\n<meta property=\"og:description\" content=\"How many times have you written this code? : Or some code like it.\u00a0 It is pretty common to parse a line and then take each column and store it in your object as data. One of the annoying problems is that if you have optional columns you have to check to see if they...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-30T14:58:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-07-14T18:52:32+00:00\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Parsing Columns Like A Ninja\",\"datePublished\":\"2010-06-30T14:58:17+00:00\",\"dateModified\":\"2016-07-14T18:52:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/\"},\"wordCount\":354,\"commentCount\":5,\"articleSection\":[\"Algorithms\",\"Best Practices\",\"C#\",\"Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/\",\"name\":\"Parsing Columns Like A Ninja\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"datePublished\":\"2010-06-30T14:58:17+00:00\",\"dateModified\":\"2016-07-14T18:52:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/parsing-columns-like-a-ninja\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Parsing Columns Like A Ninja\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Parsing Columns Like A Ninja","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/","og_locale":"en_US","og_type":"article","og_title":"Parsing Columns Like A Ninja","og_description":"How many times have you written this code? : Or some code like it.\u00a0 It is pretty common to parse a line and then take each column and store it in your object as data. One of the annoying problems is that if you have optional columns you have to check to see if they...","og_url":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/","og_site_name":"Simple Programmer","article_published_time":"2010-06-30T14:58:17+00:00","article_modified_time":"2016-07-14T18:52:32+00:00","author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Parsing Columns Like A Ninja","datePublished":"2010-06-30T14:58:17+00:00","dateModified":"2016-07-14T18:52:32+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/"},"wordCount":354,"commentCount":5,"articleSection":["Algorithms","Best Practices","C#","Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/","url":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/","name":"Parsing Columns Like A Ninja","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"datePublished":"2010-06-30T14:58:17+00:00","dateModified":"2016-07-14T18:52:32+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/parsing-columns-like-a-ninja\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Parsing Columns Like A Ninja"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=975"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/975\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}