Alternatively, you could write the loop like this:

my @interesting = (); while(<DATA>) { chomp; push @interesting, $1 if m/^(?:attribute|value): (.*)$/; } my %attributes = @interesting; # magic

It's a kind of magic. :) You could even turn this into a oneliner, using e.g. grep and map, and employing some more dirty tricks along the way:

my %attributes = grep { length } map { m/^(?:attribute|value): (.*)$/ +and $1 } <DATA>;

Anyhow, going back to the while loop version, adding support for parsing the attributes of several objects at the same time is also fairly straightforward. E.g.:

my %dataObjs = (); my @interesting = (); my $dataObj; while(<DATA>) { chomp; if(m/^AVUs defined for dataObj (.*):$/) { defined $dataObj and $dataObjs{$dataObj} = { @interesting }; $dataObj = $1; } push @interesting, $1 if m/^(?:attribute|value): (.*)$/; } $dataObjs{$dataObj} = { @interesting };

Though in this case I'd use your solution instead, since it leads to much simpler code:

my %dataObjs = (); my @interesting = (); my $dataObj; my $att; while(<DATA>) { chomp; $dataObj = $1 if m/^AVUs defined for dataObj (. +*):$/; $att = $1 if m/^attribute:\s+(.+)/; $dataObjs{$dataObj}->{$att} = $1 if /^value:\s+(.+)/; }

No magic, alas. :)

Side note - according to my favorite WWW search engine, this sort of data is generated by iRODS. CPAN doesn't have any related modules, so here's a good opportunity to contribute to the Perl ecosystem for anyone who works with that system.


In reply to Re^2: parsing metadata by AppleFritter
in thread parsing metadata by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.