{"id":6460,"date":"2011-05-23T21:45:26","date_gmt":"2011-05-24T01:45:26","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=6460"},"modified":"2012-06-04T11:54:35","modified_gmt":"2012-06-04T15:54:35","slug":"parsing-adif-with-perl","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2011\/05\/23\/parsing-adif-with-perl\/","title":{"rendered":"Parsing ADIF with Perl"},"content":{"rendered":"<p>In ham radio, we&#8217;re plagued with a data log standard called ADIF, the <a href=\"http:\/\/www.adif.org\/\">Amateur Data Interchange Format<\/a>. It certainly is amateur, in the bad sense of the word. It looks like someone once saw SGML in a fever dream, and wrote down what little they remembered.<\/p>\n<p>Anyway, the following Perl snippet will parse an ADIF file into an array of hashes. It was based on <a href=\"http:\/\/www.perlmonks.org\/?node_id=559222\">some code from PerlMonks that kinda worked<\/a>. This works for all the file (singular) I tested.<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/perl -w\r\n# modified from perlmonks - TedPride - http:\/\/www.perlmonks.org\/?node_id=559222\r\nuse strict;\r\n\r\nmy ( $temp, @results ) = '';\r\n\r\n### Fast forward past header\r\nwhile (&lt;&gt;) {\r\n  last if m\/&lt;eoh&gt;\\s+$\/i;\r\n}\r\n\r\n### While there are records remaining...\r\nwhile (&lt;&gt;) {\r\n  $temp .= $_;\r\n\r\n  ### Process if end of record tag reached\r\n  if (m\/&lt;eor&gt;\\s+$\/i) {\r\n    my %hash;\r\n    $temp =~ s\/\\n\/\/g;\r\n    $temp =~ s\/&lt;eoh&gt;.*\/\/i;\r\n    $temp =~ s\/&lt;eor&gt;.*\/\/i;\r\n    my @arr = split( '&lt;', $temp );\r\n    foreach (@arr) {\r\n      next if (\/^$\/);\r\n      my ( $key, $val ) = split( '&gt;', $_ );\r\n      $key =~ s\/:.*$\/\/;\r\n      $hash{$key} = $val unless ( $key eq '' );\r\n    }\r\n    push @results, \\%hash;\r\n    $temp = '';\r\n  }\r\n}\r\n\r\n# example: just pull out CALL and GRIDSQUARE for each record that has them\r\nforeach (@results) {\r\n  next unless exists( $_-&gt;{GRIDSQUARE} );\r\n  print join( &quot;\\t&quot;, $_-&gt;{CALL}, $_-&gt;{GRIDSQUARE} ), &quot;\\n&quot;;\r\n}\r\n\r\nexit;\r\n<\/pre>\n<p>If you want some real code to manipulate ADIF files, <a href=\"http:\/\/jaakko.home.cern.ch\/jaakko\/Soft\/\">adifmerg<\/a> works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ham radio, we&#8217;re plagued with a data log standard called ADIF, the Amateur Data Interchange Format. It certainly is amateur, in the bad sense of the word. It looks like someone once saw SGML in a fever dream, and wrote down what little they remembered. Anyway, the following Perl snippet will parse an ADIF [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2290],"tags":[2305,187,1804],"class_list":["post-6460","post","type-post","status-publish","format-standard","hentry","category-amateur-radio","tag-adif","tag-perl","tag-yuck"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-1Gc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/6460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=6460"}],"version-history":[{"count":5,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/6460\/revisions"}],"predecessor-version":[{"id":7599,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/6460\/revisions\/7599"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=6460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=6460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=6460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}