Mark Jensen

Invoke Theory. Apply Boundry Conditions

  • Lab Notebook
  • home
  • blog
  • About
  • Art
  • Events
  • References
  • MHCPC

YAML Input Processing

September 26, 2016 by Mark Jensen

Wanting to capture some simple accounting data for a local organization, I started using using YAML as the input method. The YAML string found it home in an Excel cell in a spreadsheet. The format was:

--- 
refid: 999999
refdate: 2016-01-01
acct: 8888
total: 555.55
method: General Fund
chkno: 4444
conf: 555555
cmt: This is a comment.
...

The requirements morphed into having multiple account numbers and amounts. The resulting format now was:

--- 
refid: 999999
refdate: 2016-01-01
accounts: 
  - acct: 8888
    amt: 88.88
  - acct: 7777
    amt: 77.77
  - acct: 6666
    amt: 66.66
total: 555.55
method: General Fund
chkno: 4444
conf: 555555
cmt: This is a comment.
...

The resulting Perl hash looks like:

$VAR1 = {
          'cmt' => 'This is a comment.',
          'refid' => '999999',
          'conf' => '555555',
          'refdate' => '2016-01-01',
          'total' => '555.55',
          'method' => 'General Fund',
          'accounts' => [
                        {
                          'acct' => '8888',
                          'amt' => '88.88'
                        },
                        {
                          'acct' => '7777',
                          'amt' => '77.77'
                        },
                        {
                          'amt' => '66.66',
                          'acct' => '6666'
                        }
                      ],
          'chkno' => '4444'
        };

I was not able to use the cpan module YAML to read this YAML string into the code. I was able to use YAML::Tiny to successfully read it.

⋮
# Read in YAML from file 'tmp.yml'.

use  $fn = 'tmp.yml';
my   $yaml = YAML::Tiny->read($fn);
my   $hash = $yaml->[0];

# YAML file 'tmp.yml' is available for use with $hash.
⋮
September 26, 2016 /Mark Jensen
$\setCounter{0}$
  • Newer
  • Older
blog RSS

Powered by Squarespace